Implementing Core Data with RESTKit and MagicalRecords? - ios

I am trying to implement Core data for the first time . I got to know about the MagicalRecords and RESTKit while learning about the core data. i am right now in a fix about how to implement Core data and how to use both the libraries in the same application . And is it safe and best way to use both .
In RESTKit , it need endpoints for mapping . What if i have more then one endpoints for single Entity.
How i cam be assured that i will not have duplicate data in the application.
Right now i am having request on each and every UIViewController . Fetching JSON in UIViewController itself and dumping it in Arrays .
How can i reduce the calls , use application without network. Please enlighten me with the knowledge about this .
Before down voting please comment what you are not understanding. Thanks

And is it safe and best way to use both .
Safe is all about you code and how you write it. You don't need to use MR & RK together but you can - both are conveniences to help you.
What if i have more then one endpoints for single Entity.
Create multiple request / response descriptors
How i cam be assured that i will not have duplicate data in the application.
Use core data and unique identities. Consider also using a shared data controller rather then redefining everything in each view controller.
How can i reduce the calls , use application without network.
You need to design your own scheme. Core data will help as you can run fetch requests to get existing data and make a request to update, if you use a fetched results controller it will automatically update with the results.

Related

Best design pattern to handle iOS Application State/Data

I am starting a new project (learning purposes) and I am trying to figure out what is the best software design pattern to use in the following scenario.
I have several data that need to be downloaded from multiple webservices and store somewhere in my app, to display it later. However each piece of data (e.g. list of teachers, students) will only be used in one or more specific view controllers (e.g. teachersViewController and studentsViewController).
I read that the Singleton pattern or use the AppDelegate to store a variable (an object like ApplicationData) is a bad practise, even more in this example which I want to restrict the data access.
So, which design pattern should I choose? I have read something about dependency injection, but I don't have any clue about it or if it even helps me in this question. If it helps, some examples with explanation would be nice.
You need some sort of database to store downloaded data. Good choices are Realm and Core Data. The right way to process data is:
Check if data is already in DB and show it if available.
Download or update data from server and parse it to objects.
Save objects to DB.
Show data taken from DB to user.
Download data as needed. When you open VC with students then download only students data and so on.
EDITED: If you need all the data on app open then load it and put in a DB before first screen opens. Then just use DB to show data to user.

Working with breeze.js and multi data sources

I'm trying to integrate breeze.js on my SPA .
Working with in with the entity structure comming from the DB is really straightforward.
I have a problem when I need to deal with special cases. Here is my case :
I need to get my entity list , in the first time I fill the entity from external source only for display mode (the original data exists in an external source) , when I see the data then I can choose which to approve, then I'll save the entities in my DB.
Do you know to manage this with breeze in automatic way .
Thank in advance ...
Take a look at the NoDB sample in the Breeze zip.

AFIncrementalStore to sync Core Data with REST API

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.

CoreData best practice implmentation on the UI side and in subproject

app scenario: on the UI, a button is tapped to get contact list from the server. the request goes to subproject which does the download and parsing and returns the result thru its delegate to the UI. so far everything works properly. lets say there is no internet connection and we cant have the contact list. to solve the problem, I want to cache the data in core data. if there is no internet, the cached data will be returned. now the question that bugs me, is it possible to create one data model and use it in subproject to save the data and in UI where data get pulled and edit from the same data model?
so basically i want to access core data from different subprojects and UI.
i couldnt find hints or tutorials regarding this issue. any ideas?
thanks in advance!
edit:
a project "b" that is added to the parent project "a". the project "b" is actually a static library.
if i let the library to do the saving and returning data to UI, wont it be inefficient to get all data from core data then send it to the UI?
i actually hope that there is a way to use same data model in both UI and the library.
i want prevent the UI to have huge load of data. its better to hace core data to handle that incl. memory mangement. i'm still reading some sources and trying to implement it on a test project.
I would argue that only the main project should deal with persistency, as than you can always decide to handle it differently — save it permanently or not, use core data or a home grown sql wrapper…. So it would be up the the delegate to decide what to do with more data.
But along with the delegate protocol you could decide to maintain different model protocols that define, what your models can hold. this would be independent to the implementation. The delegate now could return objects — no matter if core data models or not — to the delegator if this objects conforms to the protocols. The delegator in the sub module now could check for values on the server and/or in the cache.

Synchronization with RestKit

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.

Resources