2015-04-06 51 views
7

這是我的代碼,我無法弄清楚我做錯了什麼。我做的,因爲它的文檔 http://realm.io/docs/cocoa/0.91.1/#deleting-objectsdeleteObject在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"; 
     [EBooks createOrUpdateInRealm:realm withObject:eBookdb]; 

     [realm commitWriteTransaction]; 

     //=> break point here before crash  

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

,並與下面的錯誤

斷點後的應用程序崩潰的說:「只能刪除其所屬的領域的對象。」

回答

5

問題是您試圖刪除獨立的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]; 
+0

這是我當前的代碼http://pastebin.com/N8VhTG7M 但現在我發現了一個新的錯誤**終止,由於應用程序未捕獲異常'RLMException',原因:'索引超出範圍'*** – iDev 2015-04-06 16:49:51

+0

對不起,錯誤是由於不同的問題引起的。感謝:D – iDev 2015-04-06 17:22:12