App structure iOS and Realm: create database when app is installed - ios

I am very new to iOS. I am developing an app with data persistence. I have decided to use Realm for that purpose.
I must to create the database and load data the first time that app runs. I get data from a Web Service in JSON format. I will implement some strategy to update this database later, maybe with iOS Silent Push notifications.
I have read and I have worked about Realm, loading data from JSON... to learn about that.
Now, I need to apply this in my project but I don't know how to start. I need some clues about general idea for the app:
How can I organize my app to load data when it is installed? At what point should I create the database and load data?
I have thought to create a global Realm object y AppDelegate and use it as a global variable. Is it a good idea?
Do I need to set a path for my database? Can I user default path?

If you are looking for a place to start, you can check out the example apps of this UI component add-on for Realm: ABFRealmGridController.
The controller is a subclass of UICollectionView and the example app should demonstrate most of the functionality you are curious about. The example uses the controller to display the top news stories from the New York Times. This involves making a request to their API and loading the JSON response data into Realm.
When to load the data is dependent on how you want the app to function. If the data will be the same for each user, you could bundle the Realm file with the app pre-populated with data.
The ABFRealmGridController example loads data when the user clicks the refresh button and performs the JSON handling on a background thread; a general best-practice.
Finally, unless you have multiple Realms or need to store the file in a specific path, it is probably simplest to use the default path.

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.

Core Data Values Accessed by Multiple Users when logged in (iOS Swift)

I have an app where a UITableView is used to represent a friends list. Now, this table is updated by values stored in core data, and I'm downloading friend values to core data via Parse. Instead of accessing Parse to update the tableView directly, I decided to update Core Data and then the tableView because I need the friend information in other parts of the app, and thought it would be more efficient to use Core Data than to have calls to Parse again and again. Everything works fine!
However, my app has a log in system before users can use the app. And when I created another dummy user to test it, I found that the friend values stored in Core Data by my actual account were being used to update the friend list tableView! When actually the tableView should be empty (new user).
I don't know exactly how Core Data works but I figure it uses some segment of the device's memory to store entities. My question is this, is it possible to use Core Data to store private information related to a particular user that can't be accessed by other users that log into the same device? Or should I continue to make calls to Parse whenever I need information?
Hope you guys understood my question, thanks!
iOS is not a multi-user system. Your app stores its files in a sandboxed folder structure and this sandbox is independent of any user logins you have implemented in your app.
When a new user logs in (or, if you prefer, when a user logs out) it is up to you to remove any data you have stored in Core Data that you don't want another user to have access to.
Parse can save data offline by Local Storage or cache the request by Cache Policy
They are much faster than creating your own database (by CoreData).

Best way to check for data updates on webserver

I have an application which uses the data from web server. When you first launch the app, it downloads the data and then work with it. But what if the data on web site was changed. How can I know from the application that the data was changed, and if so, what data should I download?
My first idea was each time when you run the application to check the number of entries in the local database on your phone and the number of entries on web server, and if they are not equal, delete all data in local database and then download all data again. But I think that it will take more time than if the application just loads 5-10 needed records instead of all data.
The second idea was when the information on the site changes, website somehow has to inform the application to load some records. But I don’t know if it is possible to do(
Another idea was to compare the id of the last entry in the application database with last id on website. And if they are not equal download the information from the next id.
Are there any suggestions how can I accomplish this?
I am not sure that you have any database or web services but my suggestion is parsing data from the web with JSON or XML.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/
this class reference is will be clear for you.
Also in my opinion, if you are new in swift and want to choose easy way for this operation search for iOS package managers.
If you want to use a package manager for your project, e.g Pod
https://cocoapods.org/pods/Alamofire
would be a good startig point.
Alamofire is an HTTP networking library written in Swift.
Hope to helped you

Where to store user settings in Electron (Atom Shell) Application?

I can't seem to locate a built in mechanism to store user settings. I was hoping that electron provided a standard method for storing user settings across all desktop platforms. If there isn't a precedent for this I can implement it myself, I just didn't want to jump to a custom solution immediately. Research online is pretty sparse in this area. Thanks!
Each platform has different default locations for different kinds of data. So, if you want to store data in default locations based on platform, check out app.getPath(name)
It retrieves a path to a special directory or file associated with name.
You can also use it to differentiate between data the user wants to save, and data your application saves that you don't want to clutter up users directories.
Or if you just want to store files reletive to a specific path you can use the
app.setPath(name,path)
I've faced this particular problem with my Electron app and this post inspired me to write an NPM module called electron-json-storage.
This module allows to easily write/read JSON to/from app.getPath('userData'):
const storage = require('electron-json-storage');
// Write
storage.set('foobar', { foo: 'bar' }).then(function() {
// Read
storage.get('foobar').then(function(object) {
console.log(object.foo);
// will print "bar"
});
});
Electron doesn't give you anything out of the box for this. However, Electron does give you a method for getting the idiomatic location of storing user data in a cross platform way via the app.getPath API.
I'd say the 3 most common ways to do this are:
localStorage (or any HTML5 storage API)
flat JSON file (this is what I do, and I use electron-store for it)
embedded database like IndexedDB, neDB, or sqlite
Which one you choose will depend on your app's needs. If you only need to access this data in the renderer process, then I'd just use localStorage. Most of the time it seems you need to access the data in both the main and renderer, so a JSON file makes sense. If you're dealing with lots of data or complex querying, then maybe a database makes sense. I wrote about this more in detail here.
How about LocalStorage? If you need to access these settings from the browser process, you probably need to write your own (or just use a node.js library that implements this)
The best way that I have found is to store it in a simple file as JSON. The problem is that if you store that JSON in the app dir, then when you update the app, it will get wiped out. So you want to put it in the default directory for user settings for the current operating system. LUCKILY!!!!! There is a library for node developers that will help you find the userdata directory. The module is called appdirectory, and I have used it several times. It is extremely easy to use.
See APPDIRECTORY HERE
One could store data in cookies; Electron has a mechanism for it (https://electronjs.org/docs/api/cookies) and the cookies can be retrieved in the browser (Angular: https://docs.angularjs.org/api/ngCookies/service/$cookies, React/Other: https://github.com/reactivestack/cookies)
I was able to get it working with Angularjs.

How to split an iOS sqlite db?

I have an existing iOS app that uses core data for app data and user data. My problem is that updating app data is a nightmare (my first app, so I didn't do it ideally the first time). I would like to split the app data and user data into 2 separate sqlite dbs (or stores, correct me if my terminology is wrong).
Any tips would be appreciated.
Having two sqlite files is a good idea. The pain is splitting them now.
Create a new store that only exists in your app bundle. Make sure the data is unchanged from when you first released the app.
You are going to need to walk the "user" store and find all of the data that is identical to what exists in the "reference" store and delete it. If the user has changed that data then I would leave it and let the user sort out duplicates.
Once that is complete your app can resume normal function and load up both stores. I would set a flag somewhere so that you know this has been done and you don't run the check on every launch. The "user" store's metadata is a good place.
Note, this will need to be done before the user can "use" your app. This probably means changing your launch routines so that if a migration and filter is needed you tell the user what is going on.
I don't think having multiple persistent stores is the right solution. You can simply have separate entities within a single persistent store. Core Data will handle it properly.

Resources