gpt4 book ai didi

ios - deleteObject 在 Realm 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 03:31:40 25 4
gpt4 key购买 nike

这是我的代码,我不知道我做错了什么。我正在按照文档中的说明进行操作 http://realm.io/docs/cocoa/0.91.1/#deleting-objects

       // Delete Current Object
RLMRealm *realm = RLMRealm.defaultRealm;

[realm beginWriteTransaction];
EBooks *eBookdb = [[EBooks alloc]init];
eBookdb.eBook_ID = [NSString stringWithFormat:@"%@",self.eBookID];
eBookdb.status = @"canceled";
[EBooks createOrUpdateInRealm:realm withObject:eBookdb];

[realm commitWriteTransaction];

//=> break point here before crash

[realm beginWriteTransaction];
[realm deleteObject:eBookdb];
[realm commitWriteTransaction];

应用程序在断点后崩溃并出现以下错误

'Can only delete an object from the Realm it belongs to.'

最佳答案

问题是您正在尝试删除独立的 EBooks 对象,而不是 Realm 中持久存在的对象。如果您将代码更改为以下内容,它应该可以工作:

// Delete Current Object


RLMRealm *realm = RLMRealm.defaultRealm;

[realm beginWriteTransaction];
EBooks *eBookdb = [[EBooks alloc]init];
eBookdb.eBook_ID = [NSString stringWithFormat:@"%@",self.eBookID];
eBookdb.status = @"canceled";
eBookdb = [EBooks createOrUpdateInRealm:realm withObject:eBookdb];

[realm commitWriteTransaction];

[realm beginWriteTransaction];
[realm deleteObject:eBookdb];
[realm commitWriteTransaction];

关于ios - deleteObject 在 Realm 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29468893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com