RestKIT, Core Data and updating/deleting existing objects

4,286 views
Skip to first unread message

Matic

unread,
Jan 27, 2012, 3:26:28 PM1/27/12
to RestKit
Hello,

I would like to create an app that syncs all changes from server (and
not back to server). What is the best way to delete/update existing
object after first sync. Database has more than 30 000 records and I
would like to update few of them on a daily basis. How can I achieve
that using RestKit and Core data?

Thanks!

Matic

MARTIN Pierre

unread,
Jan 28, 2012, 7:55:39 AM1/28/12
to res...@googlegroups.com
Hello Matic,

This basically depends on the flow of your application. For instance, i doubt that all the 30k records needs to be available at first run in most of the cases, which would require you to program an "on-demand" strategy (When requiring the obejct load them).
If you really need the 30k objects available at first run, then you would probably need to establish an exchange format on first run (i used to use binary property list, it's well optimized).

About the update issue, i would recommend you to make sure the server keeps track of changes (Basically, it would just "timestamp" all objects on their last modification datetime), so when your client connects, the server just has to send the delta of modified objects based on the last update your client has.

Best to you!
Don't know if it helps,
Pierre.

Matic

unread,
Jan 28, 2012, 8:10:29 AM1/28/12
to RestKit
Hi,
thanks for reply. I will have first version of database preloaded
when the app ships. I would just like to know what is the best way to
implement updating deleting. Does RestKit have built in support for
updating deleting objects in local database?

Thanks!

Matic

MARTIN Pierre

unread,
Jan 28, 2012, 8:25:14 AM1/28/12
to res...@googlegroups.com
> I will have first version of database preloaded when the app ships. I would just like to know what is the best way to
> implement updating deleting. Does RestKit have built in support for updating deleting objects in local database?
i'm not sure, i never tried to do it "automatically" myself. i (Generally speaking in such configuration, not only for restkit) usually keep track of deleted objects IDs server-side as well as their deletion timestamp. This way, upon client connexion you can propagate the information.
The thing is, when using the connexion restkit uses to communicate with the server, you can't get "real-time" notifications from the server to the client, since only the client can initiate a connexion. So here if you are interested about real-time aspect, you would have to either keep a socket to the server open with a simple notification protocol, or maybe take advantage of push notifications.

Blake Watters

unread,
Feb 2, 2012, 9:12:33 AM2/2/12
to res...@googlegroups.com
Hi Matic and Martin -

RestKit does support automatic deletion and updating of existing records in a Core Data store. At a high level, the system relies on a connection between the resource path that your resource collection 'lives' at and a fetch request for retrieving the objects that correspond to the path. Any objects present in the fetch request but NOT in the payload retrieved from the server are considered orphaned and will be deleted.

There are obviously some limitations here (i.e. you have to send the complete payload down in one response body), but the support exists. Object updates are much easier as they only require you to identify the primaryKey attribute for your models. A managed object mapped in any response will have its existing Core Data representation updated by primary key.

There is active work underway on the underlying architecture of this feature that will permit flexibility going forward. See this branch for details: https://github.com/RestKit/RestKit/tree/rkmanagedobjectmappingcache

--
Blake Watters
VP Engineering, GateGuru
Mobile: 919.260.3783
Get GateGuru for iOS: bit.ly/ggitunes
Get GateGuru for Android: bit.ly/ggandroid

Interested in creating the mobile travel experience of the future? We're hiring!

Joe Kramer

unread,
Jun 3, 2012, 3:54:33 AM6/3/12
to res...@googlegroups.com

How do I make RestKit not delete but update existing objects? 

On every GET I receive same objects. But rest kit deleted existing objects and replaces them with identical copies.


Object mapping:


RKManagedObjectMapping* itemMapping = [RKManagedObjectMapping mappingForClass:[Item classinManagedObjectStore:objectStore];

itemMapping.primaryKeyAttribute = @"itemId";

[itemMapping mapKeyPath:@"id" toAttribute:@"itemId"];


JSON response with id:

RKResponse.m:218 Read response body: [{"id":144614501} ......


estkit.core_data:RKManagedObjectLoader.m:143 Deleting orphaned object <Item: 0xc93ad60> (entity: Item; id: 0xc935c20 <x-coredata://03AD71D5-2841-434D-8482-E840B879EC20/Item/p478> ; data: {

    itemId = 144614501;



It's same primary key!!! Identical object! Why does it do that?

It looks like RestKit creates new objects first in

RKOBjectLoader - (RKObjectMappingResult*)mapResponseWithMappingProvider:(RKObjectMappingProvider*)mappingProvider toObject:(id)targetObject inContext:(RKObjectMappingProviderContext)context error:(NSError**)error;


And then there's comparison in
RKManagedObjectLoader: (void)deleteCachedObjectsMissingFromResult:(RKObjectMappingResult*)result


Basically it comes down to this:

for (id object in cachedObjects) {

            if (NO == [results containsObject:object]) {

                RKLogTrace(@"Deleting orphaned object %@: not found in result set and expected at this resource path", object);

                [[self.objectStore managedObjectContextForCurrentThreaddeleteObject:object];

            }

        }


How can our existing CoreData object be in cachedObjects if cachedObjects was just created from received data?

Am I missing something? 



How do I make it not delete but update existing objects?

Thank you.

Sean Dougherty

unread,
Jul 11, 2012, 1:24:35 PM7/11/12
to res...@googlegroups.com
Joe,
Did you ever figure this out? I am having the same problem.

Thanks,
Sean

flashfabrixx

unread,
Jul 11, 2012, 3:26:28 PM7/11/12
to res...@googlegroups.com
Try to set the mergePolicy for your managedObjectContext to the following and check again:

[self.managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

If it doesn't work, post your code of getting the objects and your handling of the response.

WilliamX

unread,
Apr 5, 2013, 1:19:14 PM4/5/13
to res...@googlegroups.com
Hey Blake, Im having the same scenario, just that I additionally have a function call on Apps loading to check the "LAST_MODIFIED_TIMESTAMP".  I compare it with the local one saved on my NSUserdefaults, so that I can know if I need to update my local information, and then I call the Restkit functions to get the new data and then I update manually object per object in 2 nested loops.

Are you telling me, this is not necessary and I could automatize same behavior using Restkit?

All examples I've found sofar describe a way to ALWAYS ask the server if connection available and just ask Core Data when network is not present.

I am managing a Menu for a Restaurant with meals and prices that dont change too often, and I would like to keep my local Core Data Synchronised, so that I ALWAYS ask Core Data and just ask the Server when there is an update.  Do you have any tips or, could you point me the right direction for using Restkit and Core Data to achieve this task?
thkx!
-roberto
Reply all
Reply to author
Forward
0 new messages