I was wondering if you have ever used AFIncrementalStore to sync between Core Data and a REST API? And if you so, it is a good approach to use it without the Heroku Core Data Buildpack? I don't like dark magic :P
My Core Data model might not match exactly the REST API.
AFIncrementalStore works fine with a simple REST API.
You may have to override the representationForResponse method for a fine match with your models but it should be all that is needed (except from the init part of course)
My advice: make an exact match between the API and your CoreData models and then use categories to generate the data the way you want them.
I'm currently doing an e-commerce app and my API send me products with an expiration_date which is a unix timestamp. I save as it is in CoreData and then I have a category on my NSManagedObject Product methods like hoursRemaining, weeksFromNow and so on for an easier display in the UI.
For the relationships, I'm not use those for automatic fetching with AFIncrementalStore so I can't say much.
Related
I need help with the logic of my app. I am new at coding.
Currently, I am coding a expense tracker kind of app. I can add a picture and type in the details in the fields provided.
The current flow is that these data are stored straight away in Backendless. however, when i load my uitableview, it is being populated by fetching the data from backendless immediately, which makes going between the different tabs slow as it has to load from backendless each time i change the view.
While trying to solve that, I would think that storing data in core data would help me populate my table quicker, however, I am not sure of how my core data and backendless should link up. I only have swift knowledge.
Can anyone advise me on how the flow should be like and whether I can do this with backendless api or if I would require other languages in order to achieve this?
You don't require other languages, but the backendless API (at the time of writing) has no support for core data.
Backendless will only deserialise incoming data to plain custom model classes, which you can take a update into core data, but you need to write that logic.
There are a number of mapping libraries which take one object and map it into core data, looking for unique identifiers to prevent duplication in the data store. I can't say that any of them are ideal because you usually want to use a custom class with backendless and a different custom class with core data.
If you don't register a custom class with backendless then it will deserialise to plain arrays & dictionaries and this will likely be easiest to use with mapping libraries. You might need to do a bit of manipulation to get the data into a mappable format...
I am developing an app that fetches and pushes data to a web service and I would like to know How to do it right
My app lets the user create a "group" and add his/her friends to this group where everyone would participate in this group
My initial design got me with these Data Models
Group,
Member,
Contribution
I am thinking about using a java web backend with JSON to serialize the data before sending.
Now Here are my questions:
1-What is the best way to store my models locally on the client? is it Core Data?
2- I don`t wanna fetch the group and contributions data every time the user tries to view them, so I am thinking about adding a property to the group called groupSerial which will be increased with every change , In this way the client app will query the server for the serial and fetch the group if it is different from its local version
Is this a good idea and the best way to do it? if not what is?
3- I would like to fetch the data using HTTPS requests (GET for fetching and POST for updating) , Is this the best way to do it? any best practices to consider?
4- Is it a good idea to sub class the core data entity class and add utility methods to do relative work that involves the super class
For example I would have a core data entity class called Group , so I would make a sub class called XYZGroup that would have the following class methods
+(Group)fetchGroup:(NSString gid); // return the latest version of the group ,whether from local storage or the web server
+(void)newGroup:(Group)group; //push the new group to the web server and update local data
+(NSArray)getGroupsSummaries;//Return an array of meta data about joined groups
Also , I would have a class called XYZMember that would have some methods like:
+(NSString)myMemberID;
+(NSArray)getMembers:(NSArray)numbersInContacts;//Takes a list of phone numbers and returns an array of members that have the app installed
5-Do I need additional entity classes to store the data locally , for example: Do I need a core data entity class called Mygroups that would have an array of the groups the user is currently in
Thank you very much
1, if you want to store lightweight data only, json is better than core data.
2, it depends what you implement web service and what you want to export
3, you can use WSDL xml file and use WSDLParser.app to generate obj-c code.
4, up to you
5, up to you
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.
I need help understanding how to manage the API for an iPhone application that persists remote objects using CoreData. From my understanding, when the iOS app fetches the remote objects, it loads all the objects at the resource path.
For subsequent requests, I want to reduce overhead by having the web server return only objects that have been updated since the last update time. I perform this by returning objects where updated_at is newer than the last_update time of the request.
RestKit then parses and maps the modified objects to CoreData. Is this implementation the proper way of performing synchronization using RestKit and CoreData or am I missing a layer somewhere in between?
Thanks!
Typically RESTful interfaces should try and head "back to the basics". In your case I recommend using the HTTP Header If-Modified-Since. It is slightly cleaner than passing another parameter because RestKit will handle the HTTP status responses without you doing anything.
Otherwise, your method seems normal. Server synchronization is an enormous problem and there is a lot of literature and methods dealing with it. If you want to do something more complex then a quick web search will turn up a handful of methods, but your approach is what I usually end up doing.
If a user can edit data on your app, then there is the problem of synchronizing modifications made on the device. For this you typically set a object scope "modified" flag and only upload an object if it is modified.
I'm about to write a simple iPhone app that uses Core Data to store local copy of remote data that is fetched via RESTful web service. The data changes (new records being added) quite often. I came across RestKit and I'm wondering if it can do what I need. And what I need is to load all records in the beginning and then periodically download ONLY records that were added since previous check. Obviously there is no mystery about how that can be accomplished even by simply using NSURLConnection, but I hoped RestKit (probably in combination with a proper web service) would do that without me having to write all the synchronization logic. Again the key for me is that only new/changed data is fetched from the server.
I agree - RestKit can do this, we've recently used it to do something similar in a recent project. We used a last-modified-date request header to indicate the last successful 'sync' time, which the server can use to return only the records modified since that date. A http 304 'not modified' status code was used to indicate no change when appropriate.
RestKit also includes a seeding facility, so you know up front the initial data set - you can seed it as the initial database easily, and fetch the updates, even upon first use of the application.
Some information I found useful regarding RestKit & CoreData mapping - https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md, and the Google group is a good source as well - https://groups.google.com/group/restkit. Hope this all helps.
First of all: YES
RestKit handles CoreData very well. All you need to do is to provide mapping of your entities and it does the work for you.
For the second thing about selective sync, I really recommend checking StorageRoomApp it is a great, and not so expensive service that does exactly what you need.
They have a very good API that extends RestKit, it is very easy to use and their support is great. Take a look.