I currently have an api call that gives me an array of featured series for a video streaming app. If one of these series gets deleted it seems realm won't delete the object if it is gone from the JSON REST response. Core Data does it so is there something I am missing with realm?
When you check your REST API you will need to delete any objects that no longer exist. A way to do this is to check what is in Realm against the primary keys you get from your REST api. If it doesn't exist, delete that object.
This isn't something that Realm does automatically since it has no way of knowing what has been deleted on your server.
Related
Hello I have used RestKit for my iPhone application. I fetched the result from server and save into local database. But If we delete the few record from server which are available into my local database. So how I can delete those data entry using RestKit. Have any in build method into RestKit, using that we can easily handle this scenario.
Yes. You can use the addFetchRequestBlock to give a fetch request with the predicate, by which you can avoid saving unwanted objects from responses.
So, here in the addFetchRequestBlock, you can give the predicate without any condition for the corresponding URL. So it will delete all objects and will insert new objects.
Reference : http://cocoadocs.org/docsets/RestKit/0.24.0/Classes/RKManagedObjectRequestOperation.html
I have a problem.
I have iOS client app that has to allow multiple users to log in and store their data locally.
Data is synchronized with RESTful service, and the latest snapshot along with user's changes should be stored locally for all users.
Previously this app was implemented with SQLlite as data storage engine.
Now I would like to migrate to CoreData.
What do I have:
server returns me entities for current user. User ID is not sent, as the user authorizes and gets their session;
I know who is logged in an should store all data for this particular user. In order to do that I need to say CoreData to store the object for the user with ID=12345.
The problem is:
I have to tell CoreData to store the particular object associated with particular user's ID.
I need a way to somehow alter the object mapped with RESTkit - setting proper ID field for it.
This task was straight and simple with SQLlite but looks problematic with CoreData.
I am still thinking that I don't know something about CoreData asking you to help me with clarifications or useful links.
There isn't a good way to do it.
Hacky, you could add the id as a parameter in the request so you can map it back again (requires RestKit dev branch at time of writing).
Non-hacky is to update and re-save the objects returned in the mapping result.
Alternatively you could use one operation to download the JSON, then mutate it, then run another operation to map it.
I'm having an app that saves Core Data objects to the local store for immediate or later POST to an API.
My guess was that I would get duplicate objects: (1) the object created locally and sent to the POST method and (2) one that is created when mapping the object returned from the POST call.
But RestKit seems to find the original object even if I don't have any universal identification? How can this work? I can't find any of this in the source code which makes me a little bit confused and I don't know if I can depend on this.
So:
When storing objects in Core Data and later POSTing them with RestKit, do I need an UUID so RestKit can map the actual posted object with the one returned with the POST request?
Or can I depend on Rest Kit always maps the response of the POST call to the object I sent as an argument to the POST method?
When you POST an object, RestKit does not try to create a new object as the destination of the response mapping. It explicitly updates the supplied object that is being POSTed. You don't need to have a special identifier and you don't need to already have the object in Core Data.
I have a REST service running on top of my application, which returns data to my iPad app. This app is built using RestKit to sync data in and out of the iPad. I have however a webapp running as well, which allows the users to delete some data.
The current problem that I have right now, is that whenever a user logs in into the iPad app, I run a query to get the data that was last_modified/added since his last login. This allows me to have faster/shorter queries. The only problem, is that if for example an object was deleted from the DB between his last two logins, the user will still see it in his iPad.
What strategy should I adopt to have this data in Core Data deleted as well? Should I just not delete object from my DB and have instead a BOOL that says "deleted" or not, and whenever I get the last_modified data via REST, this item will appear and I will just filter it out in the iPad?
I know RestKit has a way to delete orphans objects, but since I am syncing the "last_modified" data, I don't think it can be applied here.
From your comment question:
I created a list of objects that needs to be deleted from CoreData. So for I example, I return an array of IDS that corresponds to the Users I need to delete in CoreData. How can I do such mapping with RestKit?!
You should create a mapping to NSMutableArray. If your source data is a JSON array of strings then you will need to use a nil key path mapping in order to get the strings extracted into your destination array. See this link.
As Wain suggested, I will have a list of deleted objects in the Database with a "deleted_date" field. Whenever I will fetch the latest objects, I will also fetch the latest deleted objects back.
I have an application that talks to a remote API using RestKit with an SQLite store for Core Data. When I got to the point of adding server-side searching, I was faced with having to find a way to efficiently display search results. To accomplish this, I used an NSFetchedResultsController that is tied to another in-memory store of the same schema as the SQLite store, and also backed by RestKit. When RestKit searches against the API's search endpoint, it maps the responses back to the in-memory store.
This works really well for what I need, but has now caused another issue. When I get these "transient" objects back from the server, and the user selects one, it needs to be copied into the persistent store at that point (various interactions beyond that point will load additional objects from the server and attempt to tie them back to the original object - which will fail if they're in two different object contexts).
tl;dr
Does anyone have any suggestions on how to copy an entire object (entity and relations) from one core data store to another? Or, is that even necessary? Is there an easier way to accomplish this same task using RestKit?
Would I be better served by changing my search instead, to not use an in-memory store? If so, how should I store these objects?
I wouldn't use the in-memory store. I'd just put the search results into the main store and bypass the issues with copying. I'd then have a purge that ran to clean out the old search results that aren't required any more. You might want to tag search results that need to be kept around for some reason. And you can get RestKit to do the purge automatically when each new search is performed (see "Fetch Request Blocks and Deleting Orphaned Objects" in these docs)
For your other question, about tagging. When you make the search request, the search term is in the URL. If you use RKObjectManager and routing then you can access the metadata that is provided to the mapping to get the search term and store it into the returned objects. Something like:
#"#metadata.routing.parameters.searchTerm": #"searchTerm",
See the metadata docs here.