I do not have any special configuration, i use the configuration by default.
in myModel.swift i have
import RealmSwift
class myModel: Object {
dynamic var name = ""
}
I save the new object and if you save it successful:
let realm = try! Realm()
realm.beginWrite()
let newOject = myModel()
newObject.name = "Fido"
realm.add(newObject)
try! realm.commitWrite()
i also can persist like this form:
let newOject = myModel()
newOject.name = "Fido"
try! realm.write {
realm.add(newOject)
}
but if i close the App and try to run, i do not have any object in my database, when i print allObject always the .count is 0 :( i do not have any idea that cause this issue
In my project i drag realm frameworks, i am use objc & swift
https://www.dropbox.com/s/ellhcm2xcjnvrzi/realmFrameworks.png?dl=0
my realm config is:
Realm.Configuration {
fileURL = file:///Users/sphairo/Library/Developer/CoreSimulator/Devices/3646B067-AB34-42E6-93A6-C3D4E0BEE087/data/Containers/Data/Application/82A643F6-02B9-4268-B232-6651FE21D694/Documents/default.realm;
inMemoryIdentifier = (null);
encryptionKey = (null);
readOnly = 0;
schemaVersion = 0;
migrationBlock = <__NSMallocBlock__: 0x60000005f020>;
deleteRealmIfMigrationNeeded = 0;
shouldCompactOnLaunch = (null);
dynamic = 0;
customSchema = (null);
}
Show the code you're using to retrieve the object from Realm when the app launches from cold.
@aamck
The code that i use for get all objects is:
let realm = try! Realm()
var allMyModels = realm.objects(myModel.self)
print(allMyModels.count) // output is 0
I think that the main problem is the fileURL of Realm, but i have a doubt, is possible change the path that by default has Realm in his configuration ? I am using the application directory for save some files, may be overwriting.
What happens if you put the write & retrieve methods all in one block and run it?
let realm = try! Realm()
let newOject = myModel()
newOject.name = "Fido"
try! realm.write {
realm.add(newOject)
}
var allMyModels = realm.objects(myModel.self)
print(allMyModels.count) // What does this give?
print(allMyModels.count) // output is 1
When i close the App and try run again the same block, the output is '0' :(
Do you have any logic in your app that can delete the Realm file?
When you say 'close the app' do you mean 'home button' close or 'kill from app switcher' close?
i do not have any logic in my app that remove a file from application directory, i kill the app from switcher, i only save files in the app when i download any files, i use the tool for explore a default.realm for see if exist the items saved and if exist while the app is running.
Do you have anything in applicationWillTerminate / applicationWillResignActive / applicationDidEnterBackground ?
In my AppDelegate i do not have any block:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
@end
In my ViewController i have this:
- (void)viewDidLoad {
[super viewDidLoad];
RealmTest *test = [[RealmTest alloc] init];
[test createNewRealmModel];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
RealmTest *test = [[RealmTest alloc] init];
[test printAllObjects];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
In my RealmTest.swift i have this:
import RealmSwift
class myModel: Object {
dynamic var name = ""
}
class RealmTest : NSObject {
func createNewRealmModel() {
let realm = try! Realm()
let newOject = myModel()
newOject.name = "Fido"
try! realm.write {
realm.add(newOject)
}
let allMyModels = realm.objects(myModel.self)
print(allMyModels.count)
}
func printAllObjects() {
let realm = try! Realm()
let allMyModels = realm.objects(myModel.self)
print(allMyModels)
}
}
When i run the app the output is 1 and it seems all correctly, but the issue be come when i try run again the app, if i have the app in background the items saved still exist but i close the app from switcher so i do not have any items saved :(
Have you tried resetting the simulator, cleaning Xcode then rebuilding? You might also try it on a device to rule out a simulator bug.
I executed Reset Content and Settings ... from Simulator and i cleaned my project cmd+shift+k , cmd+b, and i executed from Product->Project->press shift (Clean Build folder) but the issue is the same :(
Try this - you'll need to make a UILabel or something to display the allMyModels.count:
Same result?
I try your suggestion and the result is the same :( only when app is running the objects is persisted :( and i can see them, the output in my console is:
if i execute po allMyModels
RealmMyModel => [myModel {
name = Fido
}, [myModel {
name = Fido
}, [myModel {
name = Fido
}]
Or po print(allMyModels.count) // is 3
when i open again the app the output is 0 does not exist any object persisted
You must be deleting or modifying the Realm file in some way. Maybe @bdash can weigh in with some suggestions as I'm struggling to see how it's happening from the information you've given.
We don't have enough to go on based on the information provided here so far. @sphairo could you create a sample Xcode project along with exact steps for us to reproduce? You could send it privately to us at [email protected] if you don't wish to share this publicly. Thanks!
I solved this problem when I upgrade to the new realm version 馃懐 @jpsim @aamck @bdash
Great, thanks for the update.
@sphairo can you point out the realm version in which this issue was coming and the version that solved it because I am facing the same issue do not seem to find a possible fix for this.