我正在四处寻找一个解决方案,在我的一个应用程序中实现一个小的离线数据存储,这将是容易和快速使用。不管怎么说,我遇到了 领域 要做到这一点。但是,我遇到了一个问题,每次我启动我的应用程序时,数据库中的内容都是null。

我做所有的分配,并调用 [医]开始行动 方法。设置我的数据库变量值。然后,我将对象添加到 领域 最后 commitWriteTransaction.

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

这是密码。.

@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];

}

我还将考虑实现这一目标的任何其他选择。.谢谢!

*****更新!**这是我在我的测试中得到的,我改变了做 [医]开始行动commitWriteTransaction 在这两种方法中,但仍然不起作用。它获取我在我的应用程序中提供的值,但是当我再次访问时,如果它曾经存储,它不会从数据库中提取/获取该值。.

Screenshot of Xcode 6 showing NSLog and part of the code

有帮助吗?

解决方案

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

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

@implementation iReceiptDatabase
@end

RLM_ARRAY_TYPE(iReceiptDatabase)

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

- (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];
    }

    //...
}

其他提示

我的猜测是永远不会被调用的viewWillDisappear。我建议在每次更改数据后提出您的写事务,而不是只要查看视图即可打开,而不是在最后添加对象,您可以更改其他方法以提交数据:

- (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);
}
.

我还要考虑将收据存储在数据模型上作为int。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top