Using RestKit with Core Data I'm providing offline support when the user adds, edits or deletes objects without an internet connection by flagging the objects and saving them with Core Data.
If the internet is available again, I'll fetch all added/edited/deleted objects, save them in arrays and right know use the normal methods and loop for each item to put them on the server.
List of arrays
- fetchedAddedCompanies
- fetchedEditedCompanies
- fetchedAddedContacts
- fetchedEditedContacts
- fetchedDeletedContacts
Init method (Example for edited companies)
// Added Companies
...
// Edited Companies
for (Company *tempObj in fetchedAddedCompanies)
{
// Find keys for selected languages
[self updateAccountToServer:tempObj:addCompanyContext];
}
// ...
At the end of updateAccountToServer I post the object (self.company) with it's mapping to the server.
...
[objectManager postObject:self.company mapResponseWith:[companyMapper inverseMapping] delegate:self];
...
Question
I'm searching for a more efficient solution when the user did update a few objects and - that the special one - did add a few new objects because in contrast to all editing/deleting methods the server returns an ID for each new created object that will be stored.
1) Avoid using e.g. updateAccountToServer for each edited object and send an array of objects instead
2) Using the RKRequestQueue together with the RKObjectManager (regarding this question and a good answer)
In my opinion 1) will be hard to implement because the resourcePath for each request contains a unique ID, so I've tried 2) but missing the possibility to bind the mapping for each request using
[syncQueue addRequest:[[RKObjectManager sharedManager] objectLoaderWithResourcePath:tempString delegate:self]];
Thanks for your ideas!
Myself and a few others are actively working on a development branch to integrate this into RestKit automatically.
We are implementing a synchronization manager inside of RK that watches the NSManagedObjectContext for changes, and if the network is unavailable, queues those requests until the network comes back.
We'd love to have more help/ideas on the code, so if you could tell us more about your use case on this page:
For 1, you can encapsulate the NSArray of inserted/updated/deleted objects into a new object, then post it to the server. For this to work, you need to ensure that the correct bits are set on the modified objects for server to decipher, and also, ensure that the server can receive the object, and deconstruct it to get an array of modified objects.
来源:https://stackoverflow.com/questions/9916960/restkit-send-added-edited-deleted-objects-after-offline-storage