Realm.io Removing the realm file - ios

My question, how can I properly remove a realm file? Currently I'm doing so using the file manager, which is fine for me since the Realm file is mostly here for offline caching.
My app logs in to a web-platform, and creates a realm for this platform so you can log in to multiple. When you remove it with the filemanager however, there can still be lock files and such so I guess this dirty way of removing is the wrong way!
in Android you have the deleteRealmFile call on the RealmClass, which works fine when all references are closed. It seems there is not much security on iOS as I can just remove it like this.
So what is the proper way of handling this?
I'm not sure if realm is intended to be used like this but it's ideal for us and makes the developers happy.
I'm working in swift and have an iOS7+ requirement.

That is the proper way of deleting it. You can check the Migration example in the RealmExample project that come with the SDK and see that that's exactly how they do it, so I assume the recommended way.
let defaultPath = Realm.defaultPath
NSFileManager.defaultManager().removeItemAtPath(defaultPath, error: nil)

Related

Reusing core data model when updating app - from scratch

My question is fairly simple though I couslnt find anything specifically answering it till yet.
I have an obj-c app that I'm updating fairly often. I would like to create a new swift app with the same bundle id to replace the older one as an update. I have like 8 core data model versions within my old app that I would love to migrate to the new swift-from-scratch-app, so my users wont lose their data. Is it even possible??
Thank u
If you're using the exact same bundle ID (this is key), then from the perspective of iOS, it is considered exactly the same app. Consider your users having version 1.x of your app installed on their devices and you release 2.x using your new Swift project. Your user, nor iOS, will be able to tell this is a brand new app written in Swift.
The gotcha here is that unless you use the exact same model names, CoreData won't be able to be initialized with existing data and you'll be forced to write a custom migration for this.
My suggestion to you is to copy the xcdatamodel file from your old project and change the language of the code it generates once copied into the new project.
Then regenerate your entities in Swift and continue working like nothing ever happened.

Realm in today extension

I installed Realm manually, and it is working on my main app. But on the today extension, i can't even import RealmSwift. I apologise if this question is a newbie question, but I've been searching the web and I can't find a proper updated version. I have read https://realm.io/news/tutorial-sharing-data-between-watchkit-and-your-app/ but it's written a year ago and it does not work anymore.
So how do I go about using realm to share data between the main app and the extension?
Thanks for the help!
Even though that article is quite old (It's not even using Realm Swift! It's using the Objective-C version bridged to Swift!), the principles of using Realm with iOS 8 extensions haven't changed to the point where the information in there has become outdated.
The main points to consider:
For all intents and purposes, a Today Extension is a wholly separate app from your main app. It needs to be able to be fully self-contained in its operation.
As long as you're using the dynamic framework version, you can link one copy of RealmSwift.framework to both your main app, and the extension.
You need to make sure you've set your Realm file to be saved in the containerURLForSecurityApplicationGroupIdentifier URL. This is the only place where your main app and the extension can share files.
Because the Today extension is technically a separate app, make sure any Object subclasses you've made are included in both your main app, and your extension's 'Compile Sources' build phases.
Please let me know if you need any more clarification. :)

Start with core data in project?

I will start developing a new project for iOS. I am unsure for now if I should use core data in this project, or not. I will find out along the way, while developing (most likely with some coaching help).
Now my noob question is: Should I, while creating my new project, already check the option 'use core data'? Or shouldnt I?
Basically my question is: What is more difficult, removing core data when I checked the option and I will not been using it after all? Or adding core data when I did not check the option and I will be using it after all?
I have found it is not super difficult to add after:Implement CoreData into an existing project using Swift
But still, it seems like alot of hassle, so I now ask u!
Thanks for the answer in advance
It doesn't matter. Starting with Core Data just prepares the core data code in the app delegate and makes an empty model file, both of which are
1) easy to create yourself, or
2) copy from a new empty project started with core data later when you need it.
And if you already have it in your project, but you don't use it, it is also no problem. I found that almost all projects need core data after a while, so I usually just let it create it from the start.
Xcode just allows you to check using coredata to prepare already a coredata for you project.
BUt it is very simple to add or remove it later.JUst simple as File>create or File delete.
In a words don't worry about creating in the begining or later.

iOS 8 App Extension and Host App in separate project files. Is it possible in Xcode 6?

Currently we're using premake5 to generate our project files, as we develop multi-platform. Obviously we don't need premake to know anything about the extension (as it's iOS8 exclusive feature) but we're facing a problem: Every time we regenerate the main app project file (which happens sometimes) we're forced to manually add all the files and configure the extension, which is dull and error-prone.
Is there a way to add the app extension (a Today Widget) to a separate project? Pbxproj files aren't version control friendly as its contents seem to be shuffled every time you save. So, saving a patch file with all the changes needed doesn't seem like an alternative.
I know this is a broad question about a convenience issue, maybe seasoned Xcode devs know some trickery (or maybe it's just something obvious we're missing here).
Thanks all in advance.

Cordova Sqlite plugin is not adding the extension .db as it says in docs?

We are testing out the Cordova Sqlite plugin to see if it meets our needs for a particular app. So far our simple tests have worked just fine. However, we're stuck on this one bit. When creating the database, the documentation states:
NOTE: The database file is created with .db extension.
In fact, in our tests, the database is created and later found and readable without any extension. The output matches our naming exactly. For instance, in this case:
var _dbName = 'testDB';
var _db = window.sqlitePlugin.openDatabase({name: _dbName});
The resulting file gets named exactly that, testDB, NOT testDB.db as the documentation suggests.
Our tests have been in iOS on the simulator. My concern is that this behavior is not reliable and will change on other platforms/hardware. I'm currently unable to test on Android.
More context...
The specific issue that I'm concerned with is that one platform might expect .db as documented, and another might not. The reason why it could be a problem is that we intend on using a prepopulated Sqlite database (moved by the app to the appropriate storage location prior to openDatabase). So the naming scheme of the packaged DB would make or break the situation.
For future readers, please note... (Aug-4, 2014)
A recent release of this plugin has rendered this question moot. As stated in their release notes, the .db extension is no longer added for Android, making the behavior of this plugin more consistent across platforms. From their release notes, as of the date of this edit:
The automatic ".db" database file extension is now removed for the Android version, for consistency with the iOS & WP(8) versions
So, in looking at the SQLitePlugin.m, the open: method has...
NSMutableDictionary *options = [command.arguments objectAtIndex:0];
NSString *dbname = [self getDBPath:[options objectForKey:#"name"]];
...which would indicate the database name is whatever you are passing to it. I did not see them append a 'db' extension anywhere in the plugin. I don't believe sqlite3_open() enhances the name, so I'm pretty sure the documentation you cited is incorrect.
I'm using this plugin with android. Can't tell you if it's adding the .db or not. But I can assure you it's working just fine on android. Android also used exactly same way as you mentioned in your question.
var _dbName = 'testDB';
var _db = window.sqlitePlugin.openDatabase({name: _dbName});

Resources