Adding basic data to an iPhone application with existing (regular) models

I have a rather complicated iPhone app that relies on an API to retrieve data from a server and display it to the user. I have about 5 model classes that are used throughout the application - they just extend NSObject.

I want to add some resistance to models so that you can use some parts of the application, even if the device is disconnected - in fact, this is just illustrious caching. I want some instances of my models to be preserved - for example, items that the user has bookmarked, while others should not, for example, hundreds of search results.

Is Core Data the right solution for this? The difficulties that I see are as follows:

  • I would have to change the way I instantiate the model throughout the project. I would have to initialize them as part of the context, which does not necessarily make sense if they really come from an external API.
  • I need to be careful not to save instances that I don't need. This seems to come down to deleting the Managed Object immediately after its creation (really inconvenient) or using a separate inconsistent context for instances that I don't want to save (better, but still somewhat inconvenient).

I was hoping that I could continue to use my models throughout the application without changing the code, which should not care about durability, but this is not possible given my requirements. An alternative is to create a new set of managed objects in parallel with my existing objects and use Managed Objects for persistance objects - but such duplication never seems to be the right solution.

Should I try to train Core Data in this, and if so, how? Or should I just look at other options - sqlite3 (seems a bit complicated for what I need), user defaults (maybe not what they are for) or even serialize and write my own objects to files (it seems hack-ish).

Thanks for any input!

+7
source share
2 answers

Why not take a look at NSKeyedArciver / Unarchiver? I would say that Core Data is an obvious solution when working with very large amounts of data along with sqlite databases, but NSKeyedArchiver is what I use to save models. Check out the Apple documentation here . Using NSKeyedArchiver is pretty simple IMO, and you can store almost everything you need in it.

+3
source

Now you create data objects as subclasses of NSObject. You can use a different level of indirection to choose whether to instead create an object as a subclass of ManagedObjectModel and not use most of your application using the protocol that defines the interface that you use to access information from these objects, and then bypass objects of type id<DataObjectProtocol> . Failure to sample will be a sign of a non-local object.

Alternatively, you can create everything as a managed entity and simply store nothing but a key for data that you do not want to store locally. That way, you can know if an object exists and how to retrieve it, if necessary, but you don’t spend a lot of time or space, saving data that should remain in the cloud. A way to do this will define everything except the key in the model as optional.

I would choose Core Data because of its flexibility, utility, and optimization. It is really worth learning. I would not recommend the sqlite3 approach, the way in iOS is Core Data. If you are using some kind of file storage, you should think about how to perform batch reads and / or writes, as you may encounter some bad performance issues when doing a few small file operations.

+1
source

All Articles