Replace Realm file while app is running
To implement the backup / restore feature, I am deleting the existing Realm database file and replacing it with the new database file with the same name. However, with the app still running it does not see the contents of the new database file. If I quit and relaunch the app, it does see the contents of the new database file. Is there any way to make the app see the new contents without having to relaunch it?
- How to write audio file locally recorded from microphone using AudioBuffer in iPhone?
- Add one row to TableView each day app used
- Setting an object nil versus release+realloc
- Twitter / Fabric login button only working once
- iOS local notifications are missing action buttons
- Xcode 7 how do you refresh provisioning profiles?
Solutions Collect From Internet About “Replace Realm file while app is running”
Much like deleting a Realm file from disk, it’s only safe to replace a Realm file on disk if your application does not currently have the Realm file open.
From Realm’s documentation on Deleting Realm files:
Because Realm avoids copying data into memory except when absolutely required, all objects managed by a Realm contain references to the file on disk, and must be deallocated before the file can be safely deleted. This includes all objects read from (or added to) the Realm, all
ThreadSafeReferenceobjects, and the
In practice, this means that deleting a Realm file should be done either on application startup before you have opened the Realm, or after only opening the Realm within an explicit autorelease pool, which ensures that all of the Realm objects will have been deallocated.
The reason for this is that Realm maintains an in-memory cache of open files, so attempting to open a file that’s already open will result in a reference to the already-open file being returned. This open file will continue to refer to the original file on disk, even if it has since been replaced. Ensuring that all references to Realm accessor objects have been cleaned up means that Realm will not have an existing open file to return, and will instead open the file from disk.
In other words, you must make sure you have no references to Realm’s accessor objects (
Object instances) at the point when you attempt to replace the Realm file. You must also make sure that any references you did have have since been deallocated (i.e., they’re not lingering in a dispatch queue’s default autorelease pool).
An alternative approach that may be easier to manage is to use a different path when you attempt to reopen the restored file. Since you’re accessing a different path on disk you’ll be guaranteed to open the new file. You’ll still need to make sure you have no references to Realm’s accessor objects since otherwise you’ll get a weird mix of old and new data, but it won’t be as critical that you ensure that the accessor objects were deallocated.
- Learning to compile in xcode
- Why can't I check to see if my array of arrays contains a specific array?
- Customize the MKAnnotationView callout
- iPhone Dev – Create UIButton Manually
- How to default UILabel Font and Size using Swift
- How to List TCP/UDP Connections in iOS 4
- How do I add files to the Copy Bundle Resources build phase with an Xcode4 Project Template
- UIImageView Animation too much memory consumption
- Categories in Objective-C aren't working
- Swift Protocol Inheritance & Generic functions
- Laziness in Swift
- Erasing on UIImageView
- CGRect for selected UITextRange adjustment for multiline text?
- UIBarButtonItem causes app to crash
- How to request iOS Development Certificate in 2013?