<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Umair Ahmed on Medium]]></title>
        <description><![CDATA[Stories by Umair Ahmed on Medium]]></description>
        <link>https://medium.com/@umair0214?source=rss-474d87158db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*_Xk32Llgo_Z4GFMWH8z14A.jpeg</url>
            <title>Stories by Umair Ahmed on Medium</title>
            <link>https://medium.com/@umair0214?source=rss-474d87158db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 12:49:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@umair0214/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Merge Data In Realm ( IOS Development )]]></title>
            <link>https://medium.com/@umair0214/merge-data-in-realm-ios-development-7ca802e4a5c5?source=rss-474d87158db------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ca802e4a5c5</guid>
            <category><![CDATA[realm]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Umair Ahmed]]></dc:creator>
            <pubDate>Sat, 04 Jan 2020 13:16:26 GMT</pubDate>
            <atom:updated>2020-01-04T13:16:26.726Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Merge Realm data without using “realm schema version</strong>”</p><p><strong>Realm:-<br></strong> Realm is a database management system, available for multiple platforms like IOS, Android, etc.</p><p><strong>Objective:-<br></strong> The objective of this article is to explain how to merge data between 2 predefine realm database files without using any schema version.</p><p>Let’s assume that we have an application with a realm database for different employees.</p><p>We have the following Person class.</p><pre><strong>@interface</strong> Person : RLMObject</pre><pre><strong>@property</strong> NSInteger <strong>id</strong>; //Primary Key<br><strong>@property</strong> NSString *fullName;<br><strong>@property</strong> <strong>int</strong> dailyWages;<br><strong>@end</strong></pre><pre><strong>@implementation</strong> Person<br> + (NSString *)primaryKey {<br>   <strong>return</strong> @”id”;<br> }<br>@end</pre><p>We have 2 realm files named <strong>DBV1.realm</strong> and <strong>DBV2.realm</strong> in the application bundle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*7bl-RvVXWRqQhOysaTeeww.png" /><figcaption>Realm files in application bundle</figcaption></figure><p>In <strong>DBV1.realm</strong> , We have the following 4 members with different wages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*r_6-pOgTn6Q-ONur9-Zrbw.png" /><figcaption>DBV1.realm Data</figcaption></figure><p>Now, we are going to copy <strong>DBV1.realm</strong> from the application bundle.</p><pre>// — — Move DBV1 from MainBundle to Document Directory — -</pre><pre>NSURL *defaultRealmURL = [RLMRealmConfiguration defaultConfiguration].fileURL;</pre><pre>NSURL *v0URL = [[NSBundle mainBundle] URLForResource:@”DBV1&quot; withExtension:@”realm”];</pre><pre>[[NSFileManager defaultManager] removeItemAtURL:defaultRealmURL error:<strong>nil</strong>];</pre><pre><strong>BOOL</strong> success =[[NSFileManager defaultManager] copyItemAtPath:v0URL.path toPath:defaultRealmURL.path error:&amp;error];</pre><pre>dispatch_async(dispatch_get_main_queue(), ^{<br> <strong>if</strong>(success){<br>     NSLog(@”Launch Realm Values %@”, [[Person allObjectsInRealm:    [RLMRealm defaultRealm]] description]);<br>[<strong>self</strong> update_Version1_Data];//Update Realm Version1 Data<br>           } <strong>else</strong>{<br>NSLog(@”didFinishLaunchingWithOptions Error is %@”,error);<br>      }<br>  });</pre><p>You can see in the above code that the file is copied from the application bundle to document directory and ready for use.</p><p>We can check it’s path/URL with the following code.</p><pre>NSLog(@”Realm file path is\n %@”,[RLMRealmConfiguration defaultConfiguration].fileURL);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*ixdDmYrKq7yThFBA0Qd1Wg.png" /><figcaption>Realm database file path</figcaption></figure><p>With the Mac <strong>Terminal </strong>program,<strong> </strong>We can <strong>open</strong> its associate <strong>realm</strong> file by adding its path.</p><p><strong>open</strong> <strong>(file path)</strong></p><p>Now we will make some modifications in employees <strong>(</strong>Umair<strong>, </strong>Awais<strong>)</strong> wages. We already define these names in an array named <strong>array_increased_wages.</strong></p><pre>array_increased_wages=[NSArray arrayWithObjects:@”Umair”,@”Awais”, <strong>nil</strong>];//Need to update their wages</pre><p>With <strong>array_increased_wages,</strong> we increased their salaries by adding 600 in their existing wages.</p><pre>RLMRealm *realm = [RLMRealm defaultRealm];<br>[realm beginWriteTransaction];<br><strong>for</strong> (Person *person <strong>in</strong> [Person objectsWhere:@”fullName IN %@”,array_increased_wages]) {<br>person.dailyWages += 600; //+600 increased in wages<br>NSLog(@”%@”,person.fullName);<br>}<br>[realm commitWriteTransaction];</pre><p>The updated version 1.0 database will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*LItKsdJ3qdUfn2zBYzN48w.png" /><figcaption>Updated version 1.0 database</figcaption></figure><p>You can see that Umair wages changed from 100 to 700 and Awais wages changed from 300 to 600</p><h3>Application Version 2.0</h3><p>Suppose, we want to release the new application version 2.0. We just added some new members is starting <strong>DBV1.realm </strong>file<strong> </strong>and named this file as <strong>DBV2.realm</strong> which contains the following data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*-m3t7rZKPCFqgwXagoa_hw.png" /><figcaption>DBV2.realm Data</figcaption></figure><p>With the above image, we can observe the following points<br>(I)There is no change is <strong>Person</strong> class attributes and methods.<br>(II) Nafeel and Maaz employees are added.<br>(III) Umair and Awais values are changed as compared to the existing realm file. They were modified in previous step.</p><p>Now we have 2 options.<br> (I) Merge version 2.0 file with modified version 1.0 file.<br> (II) Add version 2.0 on the existing modified realm file.</p><p><strong>(I) Merge version 2.0 file with modified version 1.0 file<br></strong> We need to merge version 2.0 database file in such a way that.<br> <br>1-Previously Modified values Umair(700) and Awais(900) remain in the database.<br> 2-New members Nafeel and Maaz need’s to be added. There will no change in other member’s wages.</p><p>We can achieve it with the following method version2_Merge_Data.</p><pre>-(<strong>void</strong>)version2_Merge_Data<br>{<br>NSURL *version2RealmURL=[[NSBundle mainBundle] URLForResource:@”DBV2&quot; withExtension:@”realm”];<br>RLMRealm *realm_bundle=[RLMRealm realmWithURL:version2RealmURL];<br>RLMRealm *realm = [RLMRealm defaultRealm];</pre><pre>[realm beginWriteTransaction];</pre><pre><strong>for</strong> (Person *person <strong>in</strong> [[Person allObjectsInRealm:realm_bundle] objectsWhere:@”NOT fullName IN %@”,array_increased_wages]) {</pre><pre>Person *person_addupdated=[[Person alloc] initWithValue:person];<br>[[RLMRealm defaultRealm] addOrUpdateObject:person_addupdated];<br>NSLog(@”version2_Merge_Data name is %@”,person_addupdated.fullName);<br>}</pre><pre>[realm commitWriteTransaction];</pre><pre>dispatch_async(dispatch_get_main_queue(), ^{</pre><pre>NSLog(@”All Modify Objects in version2_Merge_Data are %@”,[[Person allObjectsInRealm:realm] description]);<br>});<br>}</pre><p>With the above code, we received all objects from the existing database. We filtered them by excluding Umair and Awais value and after that, we add new members there.</p><p>Here is the final realm project file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*c-hdlczODAzle204cOvDJQ.png" /><figcaption>Final realm database file</figcaption></figure><p><strong>(II)-Add version 2.0 file on existing modified database file<br></strong> We will remove the existing version 1 file and will add version 2.0 there, It is implemented in version2_Override_Data method.</p><pre>-(<strong>void</strong>)version2_Override_Data<br>{</pre><pre>NSError *error = <strong>nil</strong>;<br>NSURL *defaultRealmURL=[RLMRealmConfiguration defaultConfiguration].fileURL;</pre><pre>NSURL *version2RealmURL=[[NSBundle mainBundle] URLForResource:@”DBV2&quot; withExtension:@”realm”];</pre><pre>[[NSFileManager defaultManager] removeItemAtURL:defaultRealmURL error:<strong>nil</strong>];</pre><pre><strong>if</strong> ([[NSFileManager defaultManager] copyItemAtPath:version2RealmURL.path toPath:defaultRealmURL.path error:&amp;error])<br>{<br>dispatch_async(dispatch_get_main_queue(), ^{<br>NSLog(@”All Modify Objects in version2_Override_Data are %@”,[[Person allObjectsInRealm:[RLMRealm defaultRealm]] description]);</pre><pre>});<br>}<strong>else</strong>{<br>       NSLog(@”version2_Override_Data Error is %@”,error);<br>     }<br> }</pre><p>Now application database file will be like <strong>DBV2.realm</strong> as Imention in the previous image <strong>DBV2.realm Data</strong></p><p>If you want to see the complete code for this article, then you can download it from here.<br><a href="https://github.com/umair0214/RealmDataMerging">https://github.com/umair0214/RealmDataMerging</a></p><h3><strong>Thanks :)</strong></h3><p>Thank you for taking the time to read this article. Don’t hesitate to comment below if you have any further questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ca802e4a5c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>