How to get Realm from the server? - ios

Is it impossible to get Realm from the server?
I tried this, but it doesn't work.
var url = "http://address/default.realm"
var rlm = RLMRealm(path: url)
Error message says 'No such file or directory'.

What you're trying isn't supported in this way.
If you want to use a Realm on the server side to seed your database, you would need to download it first and save it to the local filesystem of the device to load it from there.
If you want to sync data between the device and your server or across multiple devices, then you may be interested that this is a feature, on which we are working on. Subscribe this issue to keep up-to-date about it's current stage.

Related

How to connect to MongoDB from iOS (Swift)

I have developed an iOS application using RealmSwift.
This works fine so far.
Now, as I'm getting closer to publishing into App Store, I wanted to set up some cloud configuration to be able to connect to a cloud database, but I just got totally confused.
A couple of weeks ago I saw Realm Cloud as an option. Now I only see MongoDB Realm - or something like that. I digged into a bit, and found that there are three components: Realm DB, Realm Sync and Mongo DB Atlas.
If my understanding is correct, I have to create an Atlas Cluster, on which my Realm Database will be hosted and to which I will be able to connect and sync. Am I correct?
My main problem is that I have no idea how to connect it to my existing code. I don't want user authentication or anything from MongoDB, I have my own, I basically have a DB only, which I want to sync and connect to. So, currently in the code I usually use:
let realm = try! Realm()
try! realm.write {
...
}
How can I update it to use the MongoDB in the Atlas Cloud? I went through their 'tutorials' but I'm still way too confused.
I see a Realm(configuration: Realm.Configuration) init function, but if I should use that one, how should I get a Realm.Configuration object?
Also, what does partition key means?
Thanks a lot.
Your confusion is justified. The docs and tutorials are still a work in progress and a bit disjointed. I think over time it will improve.
SO is not a good place for a full tutorial but here's a very high level overview.
A link to the tutorial - iOS Swift Tutorial
Go through the Cocoapods install
1) Your going to create a Cluster in the MongoDB console
2) Within that cluster you're create a Realm 'app'
3) Within that Realm 'app' you're going to set up:
Sync (development mode)
Users->Providers->Email/Password Authentication
Your app will have an AppId, which can be found in the Atlas console on the left, right next to the app name (it's a document button you can click on to copy).
Then, in you XCode Realm project, you'll set it up using cocoapods to install RealmSwift.
Now to your question:
how to connect it to my existing code.
Add a struct, which is the connection string to you Atlas Realm project
import RealmSwift
struct Constants {
// Set this to your Realm App ID found in the Realm UI.
static let REALM_APP_ID = "your app id"
}
then, when you want to authentication, you'll do this
let app = RealmApp(id: Constants.REALM_APP_ID)
app.login(withCredential: AppCredentials(username: username, password: password)) { user, error in
once you've authenticated, to access realm use this
guard let user = app.currentUser() else {
fatalError("Must be logged in to access this view")
}
let realm = try! Realm(configuration: user.configuration(partitionValue: user.identity!))
I've never used Realm as anything other than an embedded database inside the iOS app. If you need to store something on the server side you should implement an API that does the writing to your MongoDB (or whatever database you use). Connecting the iOS app directly to a server side database seems like an anti-pattern to me.
Realm Sync connects to the Atlas deployment as needed to synchronize the data. My understanding is you do not need to connect to Atlas directly in your code.
Documentation provides some information and a few pages down there is sample code.

Using database offline, then updating when new connection established with iPhone

I have been requested to make my app available off-line, which means storing the data collected via Api for use when no connection available. The problem is that when a new connection is made my local data may be out of date. Also, any changes made while off-line will need to update the server.
I'm aware of a method of syncing databases so that when new connection is made the data is automatically updated both ways. However, after browsing Google I've not found a definitive method of doing this.
Can anyone help point me in the right direction?
There should be a field like a time stamp to indicate last synced time. When ever connection is online go for a fetch validate against the timestamp and update the data in offline storage.
The same way, when you have updates while offline you can set some bool value to check whether data is synced or not and sync when you are online.

RestKit automatically POST/PUT when back online

Our app supports offline activity. Meaning we want to persist locally the creation of new core data objects as well as any modifications on existing objects. Then when the app goes online again we automatically push those changes (and any dependencies) up to the server.
I would think that RestKit would support such an operation, but currently when offline we store creations/modifications in a local cache. If I kill the app, those changes are not persisted. And also there is no attempt by RestKit to post those items to their originally intended endpoints.
I cannot find any documentation to support what we need here.
Is there a way for RestKit to do what we need?
If not, how do I get offline changes to persist to the disk (and not cache)? Then would it be appropriate to flag those as not uploaded to server, and then try uploading them when we are back online?
Any other important things I should consider?
At the time of writing RestKit does not support that feature.
To save to disk you need to call saveToPersistentStore: instead of just save: on the MOC.
You need to implement a scheme yourself, observing the 'online' status of the app and scanning the data store for things that need to be uploaded (which means maintaining a flag to indicate if it's happened yet).
I solved this issue by adding another field called 'updated' to my object. This field is set to true or 1 when the object is created or modified. Each time the application is started or synchronized, it iterates through the local core data copy and sends the objects with 'updated' set. On the web service, the response ALWAYS clears 'updated' to false when returning a response. This works well in the case where the web service and app are both online.

Confused about parse local datastore & cache

I’m developing a iOS App and I want to have a level of offline support and I’m struggling out of local datastore or cache which approach to use as It appears that you can’t use these two feature together.
My query is quite basic and doesn’t change only the data that is retrieved can change.
if i used one of the cache policies, i get connection errors and nothing appears to be returned from the cache.
The workflow i’m after is on the lines of below.
->When connected to the internet perform query and store objects locally.
->if there is no internet retrieve previously downloaded objects.
For the workflow you describe I think you're looking for a cache. If you would like the user could modify the data without connection and then, when there is wifi again, synchronise the local data with the remote data then you'll need the local datastore behavior.
The problem for me is when you want both in different parts of the same app because in parse in you use local datastore you can't use the cache. I don't really understand why!

IMAP Client Sync local messages Server?

What's the best general technique for creating an IMAP client and keeping its local message store in sync with the server?
I guess I'm looking for the right way to figure out what's changed in an IMAP folder on the server since the last time I checked, and download those changes, to persist them to my local database... This would include messages no longer in the folder (deleted or moved), new messages, and changed messages...
I guess new messages is easy, I can grab the highest UID i have for a folder and then find messages since that UID. I'm not so sure about detecting messages that were deleted or moved though, or changed (maybe some flags changed on a message).
Thanks!
For sync, probably you need each folder all messages UID and flags.
You can compare local cached UIDs to server returned, with this you can dedect new messages and deleted(
Probably you should use some kind of hastable for search/compare, this will speed up all.

Resources