Philippe Sabourin
Philippe Sabourin

Reputation: 8075

RestKit and Core Data: Best Practice

I haven't really been able to find any documentation about what the best practice is for using RestKit with Core Data. Should I be saving the objects to the context before posting them, or only saving them when the post is successful? How should I handle errors posting or updating objects? What's the best way to keep your core data storage in sync with what's on the server (for example, if something is deleted on the server)? Say you're creating objects locally because you're offline, is there a good way to sync those back up to the server once you're online again? It seems refreshing will delete even local objects that have never been posted if you're using the RKObjectCache.

Upvotes: 0

Views: 390

Answers (1)

Wain
Wain

Reputation: 119021

If you create a managed object that you want to post, set the basic properties and save it. This is now effectively a stub object that RestKit will fill in with the received response. If you don't want a stub object then post a non-managed object instead.

Handle errors by detecting and processing them. That often means using RKErrorMessage, but it depends very much on what the server responses contain...

If something is deleted on the server, use a fetchRequestBlock.

You need to handle new object upload to the server. Maintain a status flag and / or a timestamp so you can process items when the network connection is regained.

RestKit will only delete objects you ask it to. If you have objects that haven't been uploaded and you use fetchRequestBlocks then the fetch should include a predicate that excludes the pending upload items from being deleted.

Upvotes: 2

Related Questions