Core Data creating new objects Swift - ios

New to iOS development..
Basically what i'm trying to do is hard code data into my app using core data. Two different types will be stored, both strings: quotes and authors (There will be around 20 of each in the app). I have a button on my Storyboard as an IBaction that when clicked will populate a label with a new quote and author. My struggle right now is 1) finding out how to create this data and 2) how to fetch this data.
Every resource i'm finding online goes through tutorials on how to store data that a user typed in and then fetch it from a button. I'm trying to figure out how to store data without user input and then fetch it. Any help is appreciated. Sorry for asking what I am sure is a pretty simple question.

Unfortunately, though this would seem like a simple task, to my knowledge there is know "built-in" way to prepopulate CoreData. You can build up a core database and then copy it into an app, or possibly use a third party solution, though. Searching for "prepopulate coredata" here and on google should give you a start.
Here's a couple threads that might help:
Any way to pre populate core data?
iOS CoreData - prepopulate db with existing indexes

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.

Saving and Filtering Favourites from a UITableViewController without Core Data

I am building a simple informational based app that provides the users with built in Leaflets and Images to view, on a variety of topics. It's a UITabBar -based application where the first tab is a list of Leaflets, the second is a list of Videos and the third is a list of Languages. Each tab is represented by a UITableViewController.
I would like to set up a favouriting mechanism where users can swipe across any leaflet or video in the UITableViewCell and "favourite" it. Whichever they favourite will then get added to the 4th tab called (unimaginatively), Favourites.
I'm not using Core Data with this application because there's no real need to do that and because I'm quite new to this concept, I'm wondering on the best way to save up to 50 favourites. How would I store this? Would I use something like a NSUserDefault for example?
It's a broad question because I've looked online and I'm not quite sure how to approach this.
I would really appreciate if anyone had any guides or thoughts on how to firstly save this and secondly, how to fetch the favourites for the favourites tab. Would/Could I use something like a NSFetchedResultsController even though I'm not using Core Data?
Should I just use Core Data with this, to save the favourites?
Any thoughts on this would really be appreciated.

(Swift iOS) Storing Article Content (image/paragraphs) locally

I'm planning out a sort of reference book application. For each topic there will be a page with an image and text stored. I don't want to create new views in xcode for each page since there are 100+ topics, I'd rather find the easiest way to store the items in a database and then call the content to display on a view template when the user selects the topic from a list. After searching around I see that this is potentially done with Core Data or SQLite, and maybe even json, but I have not encountered a clear answer.
What's the best way to handle this sort of data?
You should create a database in Core Data and where you'd like to store images, use the response from this tutorial (conversion to Swift is left as an exercise for the reader) and store the fileName as a string.
Don't use json to store 100+ items, it will be very slow. SQL is quite fast, even though it's a mobile device.

How do I perform Core Data Migration to an existing app, if all I'm changing is the SQL Db's content?

I've checked a lot of sites and answers and I can't find any solutions specific to my problem.
I don't need to change the schema for my Core data model, all I need is to modify (add some) content to the current backing SQL Database.
Any direction on this will be welcome. Thanks.
PS: I tried Apple docs and they were about as useful to me as sunshine on Mecury.
Also go easy please, I'm a beginner.
Thanks.
UPDATE;
To shed more light on my issue, my app works as thus. I have preloaded static information on the app that can't be changed by the user, each day has new content. Every month, I push an update with entirely new content specific to that month. However, when my app entered production, upon the update I pushed for this month, my users were complaining that they couldn't access the month's data. This led to me spamming them with Push notifications to have them delete the app and do a fresh install to access the new data.
How can I fix this issue? my schema stays the same, only the data changes.
If I understand correctly you want to pre-fill a Core Data database ?
If you don't care about pre-existing data on existing app, you can make an iPhone or Mac app with the same model, and let it generate the database, like explain here (Any way to pre populate core data?) it's also the way recommended in a really great book if you want to learn more about Core Data (http://pragprog.com/book/mzcd2/core-data).
Do not ever make SQL request directly, Core Data work in his own magic way.
Don't work on the SQLite-Database directly. Change all your Data through NSManagedObjectContext! To find a good strategy look up examples from Batch-Importing.
Update: You could actually have two PersistentStores (one with just static data (readonly) and the other one with user-generated data). You could interchange the readonly which you prefilled with a commandline util and downloaded from a server. You cannot have direct relationships between those two store though.
I would say that it depends on the amount of data in this prefilled store wether you should go this way or just use a plist and reference some string constants in your user data store. Try to do it with a plist as this is the simpler approach.

iOS: Update preloaded database with user-data mixed in

I've looked all over for an answer, but it seems like I'm missing something obvious. I've made a rather complex Core Data app before, but the answer to this question has eluded me for the past few months.
Here's the problem:
1) I have about 20 entities in my Model.
2) Some of these entities have user-editable objects, others have pre-loaded data
3) I would like to know if it's possible to update the pre-loaded entities with each new app update.
I know I can do this the "manual" way by specifying each updated attribute, but this is way too cumbersome. I want to just update all the pre-loaded entities once the user opens an updated version of the app. I don't want to touch the user-data.
Thank you so much for your help!
You could have a version number field in your schema which you can use to associate a version number with each record. If it has a value, it's a preload. Then for the preload stuff just insert the new data when the app opens, and then ignore/delete the old. Seems simple enough.
UPDATE:
The other alternative I believe is to separate your preloaded data into an entirely different data store. I have an app wherein I do this by delivering my preloaded data via a custom SQLite file, and user data in a CoreData store. I can do this because my preloaded data is read-only, which saves me from needing to copy the SQLite file into the documents directory. What this means is that, at every update, the new data file automatically overwrites the old by virtue of the app installation. The user's data is maintained as it should be.
Of course if your preloaded data is not read-only, then there's no way around the need to write code. In this case there's not much I can do for you, not having any more details about your problem.

Resources