Import inital data from realm database file in Swift - ios

I have exported realm database file (.realm) includes initial data for my app. I have use it for my Android app and I willing to use that initial database for my iOS app too. Can anyone help me how to import and use exported realm db file inside my iOS project using Swift in proper way?
Thanks.

Put your exported realm file in the Bundle Resources
let config = Realm.Configuration(
fileURL: Bundle.main.url(forResource: "filename", withExtension: "realm"),
readOnly: true)
let realm = try? Realm(configuration: config)
The realm file has to be read only.

Related

How can I access realm data saved on iPhone from Apple Watch

I saved some data with Realm on my iPhone, and in AppDelegate, I wrote
var config = Realm.Configuration()
let container = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.XXXXXXXX")
let realmUrl = container!.appendingPathComponent("default.realm")
config.fileURL = realmUrl
Realm.Configuration.defaultConfiguration = config
Also, in ExtensionDelegate, I wrote the same.
But when I tried to access data from watch, it shows nothing.
I printed the URL and found that iPhone saves data in
"Developer/CoreSimulator/Devices/21B1330F-3B9D-4A45-9EE4-0D06F9A48866/data/Containers/Shared/AppGroup/3697965E-9A56-447E-B0EB-BC1722DB0BD0/default.realm"
But the watch saves in
"Developer/CoreSimulator/Devices/B190D8C9-A6D8-4BE1-AD8D-7D2805BFA739/data/Containers/Shared/AppGroup/3E621FF7-86FA-4C49-9AC1-20C6C198CB2F/default.realm"
I'm now wondering how to access Realm data saved in my phone from my watch
Now solved by transfering the whole realm file with WatchConnectivity

Swift - Realm mobile platform today extension

I'm building a Notes app with today extension.
I want to link my app's data with today widget using App Group.
To do this, I have to change the default realm's fileURL to App Group URL.
Without syncConfiguration it works very well, but when I use this, the default realm's fileURL did not changed.
So my Questions is how can I change Realm.Configuration.defaultConfiguration.fileURL using realm mobile platform!!!
let file = FileManager.default
let directory: NSURL = file.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myapp")! as NSURL
let realmPath = directory.appendingPathComponent("default.realm")
Realm.Configuration.defaultConfiguration = Realm.Configuration(
fileURL: realmPath,
syncConfiguration: SyncConfiguration(
user: user,
realmURL: URL(string: "realm://myserver:9080/~/myNote")!
),
objectTypes: [realmNote.self, Tag.self]
)
self.realm = try! Realm()
please help!!!...
If you set a sync configuration on the Configuration object, you cannot set a custom file URL. The sync configuration, file URL, and in-memory identifier are mutually exclusive settings. This is by design.
If you need it to work otherwise, please feel free to file a ticket at our GitHub repository, and we'll take a look.

How to access a computer file on a phone

I am using
let data = try?
NSString(contentsOfFile: "/Users/BenA**** 1/Desktop/textFile.txt",
encoding: String.Encoding.utf8.rawValue)
to access a file on my computer. When I test put the app on my phone, it obviously didn't work because it didn't have access to the file. How could I put this file on my phone and be able to access it from there? Thanks.
Include your "textFile.txt" in main bundle by drag and drop file in to xcode project, then access it by
class testViewController: UIViewController{
var data:String = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt")
data = try? NSString(contentsOfFile: path!, encoding: ENCODING)
}}
Try this way to access 'data' in rest of code.
Hope this help.
To simulate the behavior of iOS enable the sandbox on macOS.
Now you can access files only in the container of the app.
Nevertheless the predefined folders like Documents, Library etc. still exist.
To have access to that directories there a method of NSFileManager. For example one of the most important directories in iOS is the Documents directory.
let documentsFolderURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomain: .UserDomainMask).first!
You have to use that method each time you need to get the URL of that particular directory because for security reasons the absolute path will change.

I cannot get "default.realm" file in my iOS Simulator

I want to access the Realm data file which is created on iOS Simulator. With the instruction of this question, I got to the following directory:
~/Library/Developer/CoreSimulator/Devices/0AB2D954-A3D5-4ABF-A5C5-CC470FB4ABDE/data/Containers/Data/Application/7192723E-7582-48EB-8B66-14E1073D7CE6/Documents
However, the directory has only the following Realm files:
default.realm.note
default.realm.log_a
default.realm.log_b
default.realm.log
default.realm.lock
and I cannot find the Realm file default.realm.
Also, when I open up the Realm Browser and access to the directory, all the files above except default.realm.note is grayed-out and I cannot access the Realm data.
Note that I create a Realm instance and save data by realm.write:
try! self.realm.write {
for (_, value) in json { // json is from SwiftyJSON
let pitcher = Player()
player.year.value = value["year"].int
player.name = value["name"].stringValue
self.realm.add(player)
}
}
So how can I access the Realm data file?
Realm's auxiliary files (.note, .lock, etc) are always created next to the .realm file itself. So it seems like something is deleting the realm file without deleting the auxiliary files, which would explain why you can't find it.
Please make sure you're not deleting this Realm file somewhere in your code.

.realm file automatically backed up by iCloud

my app was rejected because of the size of the content that it uploads to iCloud. The only file in my app's Documents folder is the default.realm database file. I think that this is the file that iCloud is uploading. How can I prevent iCloud to upload the database to iCloud?
Thanks.
According to the App Backup Best Practices section of the iOS App Programming Guide, <Application_Data>/Library/Caches or <Application_Data>/tmp will not backup to iCloud. Generally, you can use <Application_Data>/Library/Caches directory to save your data that you won't backup to iCloud.
To change the file path of Realm, you can pass the path parameter when creating Realm instance, like below:
let realmPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] as! String
let realm = Realm(path: realmPath.stringByAppendingPathComponent("data.realm"))
Otherwise, you can use NSURLIsExcludedFromBackupKey file system property to exclude files and directories from backups (See Technical Q&A QA1719). If you want to use the default path, there is the only way to exclude Realm file from backups.
let realm = Realm()
if let realmPathURL = NSURL(fileURLWithPath: realm.path) {
realmPathURL.setResourceValue(NSNumber(bool: true), forKey: NSURLIsExcludedFromBackupKey, error: nil)
}
Looks like the URL API has changed since the previous answer was posted. This is how you can disable the backup now:
let realm = try! Realm()
guard var url = realm.configuration.fileURL else {
return
}
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? url.setResourceValues(resourceValues)

Resources