为什么我的Realm对象没有保存存储的值? [英] Why is my Realm object not saving stored values?

查看:108
本文介绍了为什么我的Realm对象没有保存存储的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览寻找一种解决方案,在我的一个应用程序中实现一个小型离线数据存储,这将很容易和快速使用。无论如何,我遇到了 Realm 来做这件事。但是,我遇到的问题是,每次启动应用程序时,数据库中的内容都为空。

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 to do this. But, I'm getting a problem that everytime I launch my app the content in the database is null.

我完成所有分配并调用 beginWriteTransaction 方法。设置我的数据库变量值。然后,我将对象添加到领域,最后添加 commitWriteTransaction

I do all the allocation and call the beginWriteTransaction method. Set my database variable value. Then, I add the object to realm and finally commitWriteTransaction.

所以,我做一个 NSLog 来查看该值是否实际设置正确(在我的应用程序中更新之后)。但当我关闭我的应用程序或停止并在xcode iphone5模拟器中再次运行它。我尝试在viewDidLoad方法中将数据库中的值设置为我的应用程序中的全局变量。我执行NSLog来检查值是在我的数据库中还是在全局变量中,但它显示为null,这意味着没有得到存储/保存。

So, I do an NSLog to see if the value is actually set properly which it is(after updating it within my app). But when I shut down my application or stopped and run it again in the xcode iphone5 simulator. I try to set in the viewDidLoad method the value from database to a global variable in my app. Which I perform a NSLog to check if the value is either in my database or the global variable but it's display as null, meaning that is not getting store/saved.

这是代码..

@interface iReceiptDataBase : RLMObject

@property NSString* receiptNo;

@end

RLM_ARRAY_TYPE(iReceiptDataBase)

@implementation iReceiptDataBase

@end

//******** View Controller Implementation ************

- (void)viewDidLoad {

    self.realm = [RLMRealm defaultRealm]; // property type RLMRealm
    [realm beginWriteTransaction];

    self.myDataBase = [[iReceiptDataBase alloc] init]; // property type iReceiptDataBase
    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

    myDataBase.receiptNo = [NSString stringWithFormat:@"%d", receiptNumber];

    NSLog(@"In my realm database(second call) -> %@", myDataBase.receiptNo);

}

- (void)viewWillDisappear:(BOOL)animated {

    [realm addObject:myDataBase];

    [realm commitWriteTransaction];

}

我还会考虑任何其他选项来实现这个目标..谢谢!

I will also take in consideration any other options to achieve this.. Thanks!

*****更新!**这是我在测试中得到的,我改为 beginWriteTransaction 两种方法中都有 commitWriteTransaction ,但仍无法正常工作。它获取了我在我的应用程序中提供的值,但是当我再次访问它时,它不会从数据库中提取/获取该值,如果它曾经存储过..

*****UPDATE!** This is what I get in my test, which I changed to do beginWriteTransaction and commitWriteTransaction in both methods, but still doesn't work. It gets the value that I provide within my app but when I access again it doesn't pull/fetch that value from the database if it was ever store..

< img src =https://i.stack.imgur.com/0CdXS.pngalt =Xcode 6的屏幕截图显示了NSLog和部分代码>

推荐答案

你的领域对象的问题是你没有查询你的对象的领域。相反,您只是分配一个新的 iReciptDataBase 对象。您首先需要向该对象添加一个属性,以便您可以查询它,如下所示的 databaseId

The problem with your realm object is that you are not querying realm for your object. Rather, you are only allocating a new iReciptDataBase object. You will first need to add a property to that object so that you will be able to query for it, something like databaseId shown here:

@interface iReceiptDatabase : RLMObject
@property NSString *receiptNo;
@property NSString *databaseId;
@end

@implementation iReceiptDatabase
@end

RLM_ARRAY_TYPE(iReceiptDatabase)

然后在viewDidLoad中,首先查询realm文件中的现有对象,然后只有在找不到它之后,才会分配它:

Then in your viewDidLoad, you first query the realm file for an existing object, then only after not finding it, you would allocate it:

- (void)viewDidLoad {
    [super viewDidLoad];

    RLMRealm *realm = [RLMRealm defaultRealm];
    iReceiptDatabase *myDatabase = [[iReceiptDatabase objectsWhere:@"databaseId = '1'"] firstObject];

    if(!myDatabase) {
        [realm beginWriteTransaction];
        myDatabase = [[iReceiptDatabase alloc] init];
        myDatabase.receiptNo = @"1";
        myDatabase.databaseId = @"1";
        [realm addObject:myDatabase];
        [realm commitWriteTransaction];
    }

    //...
}

这篇关于为什么我的Realm对象没有保存存储的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆