Why is my Realm object not saving stored values?

后端 未结 2 960
野性不改
野性不改 2021-01-21 07:15

I was browsing around looking for a solution to implement a small offline data storage in one of my apps which would be easy and quick to use. Anyways, I came across with Realm

2条回答
  •  再見小時候
    2021-01-21 07:51

    My guess would be viewWillDisappear is never being called. I would recommend committing your write transaction after each change to your data rather than leaving the transaction open as long as the view is visible - instead of adding the object at the end, you could change your other methods to commit the data:

    - (void)viewDidLoad {
    
        self.realm = [RLMRealm defaultRealm]; // property type RLMRealm
    
        [realm beginWriteTransaction];
        self.myDataBase = [[iReceiptDataBase alloc] init]; // property type iReceiptDataBase
        [realm addObject:myDataBase];
        [realm commitWriteTransaction];
    
        receiptNumber = [myDataBase.receiptNo intValue];
    
        NSLog(@"In my realm database(first call) -> %@", myDataBase.receiptNo);
        NSLog(@"In my local app(first call) -> %d", receiptNumber);
    }
    
    -(void)drawPDF:(NSString*)fName {
    
        receiptNumber += 1; // property type int
    
        [realm beginWriteTransaction];
        myDataBase.receiptNo = [NSString stringWithFormat:@"%d", receiptNumber];
        [realm commitWriteTransaction];
    
        NSLog(@"In my realm database(second call) -> %@", myDataBase.receiptNo);
    }
    

    I would also consider storing receiptNo as an int on your data model.

提交回复
热议问题