I want to create an offline iOS application using Realm. Users just need to read the database, they don't need to update or create entries.
I can create my pre-populated database using Realm Studio and I need to use that database file in my final app so users can access the database with the same data. For example, jobs.realm file with same data should be available to the users when they first launch the app.
I've been searching for a solution but couldn't find anything I can understand.
Follow the documentation for bundling a realm file: https://realm.io/docs/swift/latest/#bundling-a-realm
Related
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.
I am working on turning an web app into an iOS app.
They both use the same backend/restAPI.
My question now is: Does core data have any function that makes it possible to pre populate my ios apps "database" ?
I have a few sql files with relational tables (one-to-many) that I would want to get in my app.
Eg, state/city, category/sub-category.
So is there a way to get the data form my sql file into the ios app?
I would want to build/ship the app with the data already there, instead of making an API call and then store the data.
Thanks
Do you really need Core Data? If all you need is access to your relational data, I'd create a SQLite database from your SQL dumps on your PC. Then you can include that file in your app's bundle during the "Copy Resources" step. SQLite is available on iOS, so you'd be good to go.
I am currently working on the app that is going to use database to store items. The first thing that everybody say, when it comes to store data in iOS is Core Data.
But, after few days of looking through tutorials and docs, I have a big question.
Let me explain architecture a little bit more. So we have a backend, where you can add items, also, we have iOS and Android application. I am creating a Core Data model for our database.
What we want,is to check if there is update for database and download it. The problem is that we don't use JSON or XML, we are using the new sqlite file.
Since Core data creates three files for database, which are:
db.sqlite
db.sqlite-wal
db.sqlite-shm
Is core data able to replace "db.sqlite" with the new one, that is downloaded from server?
Thought the idea of replacing the database file instead of importing objects to it is very tempting it's highly discouraged to mess up with sqlite database created by CoreData. You should never touch it manually, when you do, you'll very likely end up with broken DB or messed up data.
So no, CoreDate is not able to replace underlaying sqlite file. You should instead import your data using CoreData stack, that's how it's designed. Creating JSON/XML service is the best way you can go.
BUT IN THEORY and in case you would be able to keep CD internal information stored in the db untouched, it should be possible to replace the sqlite file. If your database is read-only for users, it might work, but if users are able to create or modify records, forget about it right now.
First, you'd have to tear down all CoreData stacks (Managed Object Context, Persistent Store Coordinator, Managed Object Model) that might be using it before you replace it, replace the file and re-create CoreData stack(s).
db.sqlite is the main database file, the other two are temporary files, Write ahead log (wal) and Shared memory (smh), so you don't need these two.
Remember you never MUST NOT change the structure of the database, just data in it!!!
However, as stated in the beginning, I do not recommend this approach at all.
I'm trying to build my first Swift app and I think Realm may be a good option for my database. This might be a totally stupid question, but will my users be able to access the data on my database without an internet connection? I'm fairly certain that the answer is yes, but I just want to make sure.
As a side note, I want the data to be stored on the users phone (not a server or anything like that)
Thanks for the help
Yep! Realm is a completely offline, local database solution. There's no online component, but if you do decide to, you can sync data from Realm online using third party cloud services like Parse (Or just literally copying the database file to Dropbox).
By default, all data saved with Realm is stored in a file called 'default.realm' in the Documents directory of your app, but you can easily explicitly set where you want the data to be saved.
My iPhone app is required to work offline, so I need te retrieve the db from the server into the devise.
The database contains about 10 tables with a few dozen lines each.
What is the recommended way for doing so?
Is it the sqlite3?if so, how can I build the sqlite tables from the DB?
Thanks,
Asaf
You have multiple options:
download a sqlite database file from webserver, store it in your app's document or temp folder
create the database and tables dynamically by code, set up an XML or JSON interface on your webserver, download the data from web and insert to the new database
Take a look here for a beginning with SQLite on iOS.
And yes, it is sqlite3 on iOS.
I ship my apps with the tables built, with some data, then have the app call a web service that takes "last updated" as a parameter, and returns all additions/deletions since then (as JSON)...the app then parses that, and inserts/updates/deletes as needed