[realm-cocoa] Can't set primary key property 'id' to existing value 'temporaryID'

已查看 398 次
跳至第一个未读帖子

Henry Kim

未读,
2015年12月17日 06:33:382015/12/17
收件人 Realm
Hi I'm using RealmSwift on my project.
but I faced the trouble when I create Object in a realm using same 'primary key'

here is my code then I don't know why the error kill my app even update field is true!

class BDRHomeView: Object {


   ......


    let events = List<BDRHomeEvent>()

    dynamic var id = "temporaryID"

    

    override class func primaryKey() -> String? {

        return "id"

    }

}




...

realm.create(BDRHomeView.self, value:response, update:true)

....





here is the error : 


*** Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'id' to existing value 'temporaryID'.'



could you help me?  and I use Realm-Swift 0.96.3 



Marius Rackwitz

未读,
2015年12月17日 14:25:042015/12/17
收件人 Henry Kim、realm...@googlegroups.com
Are you sure that the exception you're facing is originating from that particular line. Or do you have perhaps somewhere else some code around where you modify the id property of an already persisted object?
Can you show us where the variable response is coming from and some more context around that code and share the full stack trace?

--
Marius Rackwitz
iOS Product Engineer
{#HS:150818629-2495#}
--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/4e980a9f-6220-457e-bab9-64cfd4b946d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Henry Kim

未读,
2015年12月17日 21:07:052015/12/17
收件人 Realm
here is sample code




import RealmSwift


class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        for (var i = 0; i < 2; i++ ) {

            do {

                let realm = try Realm()

                realm.beginWrite()

                let response = ["test":"test1"]

                realm.create(BDRHome.self, value: response, update: true)

                try realm.commitWrite()

            } catch let error {print(error)}

        }

    }

}



class BDRHome:Object {

    dynamic var id = "temporaryID"

    dynamic var test:String?

    override class func primaryKey() -> String? {

        return "id"

    }

}



2015-12-18 10:50:15.366 RealmTest[63007:20816519] *** Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'id' to existing value 'temporaryID'.'

*** First throw call stack:

(

0   CoreFoundation                      0x000000010f3fdf45 __exceptionPreprocess + 165

1   libobjc.A.dylib                     0x0000000111121deb objc_exception_throw + 48

2   Realm                               0x000000010ef0f2e2 RLMDynamicSet + 4802

3   Realm                               0x000000010ef2782c RLMCreateObjectInRealmWithValue + 2825

4   RealmSwift                          0x000000010ee8aa3b _TFC10RealmSwift5Realm6createuRdq_CS_6Object_fS0_FTMq_5valuePSs9AnyObject_6updateSb_q_ + 1659

5   RealmTest                           0x000000010edc741f _TFC9RealmTest14ViewController11viewDidLoadfS0_FT_T_ + 767

6   RealmTest                           0x000000010edc7792 _TToFC9RealmTest14ViewController11viewDidLoadfS0_FT_T_ + 34

7   UIKit                               0x000000010fdaccc4 -[UIViewController loadViewIfRequired] + 1198

8   UIKit                               0x000000010fdad013 -[UIViewController view] + 27

9   UIKit                               0x000000010fc8651c -[UIWindow addRootViewControllerViewIfPossible] + 61

10  UIKit                               0x000000010fc86c05 -[UIWindow _setHidden:forced:] + 282

11  UIKit                               0x000000010fc984a5 -[UIWindow makeKeyAndVisible] + 42

12  UIKit                               0x000000010fc12396 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131

13  UIKit                               0x000000010fc189c3 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1750

14  UIKit                               0x000000010fc15ba3 -[UIApplication workspaceDidEndTransaction:] + 188

15  FrontBoardServices                  0x0000000113efa784 -[FBSSerialQueue _performNext] + 192

16  FrontBoardServices                  0x0000000113efaaf2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45

17  CoreFoundation                      0x000000010f32a011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

18  CoreFoundation                      0x000000010f31ff3c __CFRunLoopDoSources0 + 556

19  CoreFoundation                      0x000000010f31f3f3 __CFRunLoopRun + 867

20  CoreFoundation                      0x000000010f31ee08 CFRunLoopRunSpecific + 488

21  UIKit                               0x000000010fc154f5 -[UIApplication _run] + 402

22  UIKit                               0x000000010fc1a30d UIApplicationMain + 171

23  RealmTest                           0x000000010edc93cd main + 109

24  libdyld.dylib                       0x0000000111d0892d start + 1

25  ???                                 0x0000000000000001 0x0 + 1





but when I changed code as below



let response = ["test":"test1"]

realm.add(BDRHome(value:response), update:true) 




then it does work! 



what's the problem with create code 





2015년 12월 17일 목요일 오후 8시 33분 38초 UTC+9, Henry Kim 님의 말:

Marius Rackwitz

未读,
2015年12月21日 12:56:002015/12/21
收件人 Henry Kim、realm...@googlegroups.com
The problem here is that the methods with createOrUpdate semantics derive what they should do from the value provided.
Because there is no primary key given in the value, it is assuming that a new instance should be created. That doesn't cover your case, where a default instance exists implemented via the default value of the primary key. I'd suggest that you open up an issue on our repo, so that we can discuss over there how that case should be handled. We could at least fail more graceful, when we decide that there are good reasons against supporting that.


--
Marius Rackwitz
iOS Product Engineer
{#HS:150818629-2495#}
On Fri, Dec 18, 2015 at 2:07 AM UTC, Henry Kim <he...@vcnc.co.kr> wrote:
here is sample code




import RealmSwift


class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

for (var i = 0; i < 2; i++ ) {

do {

let realm = try Realm()

realm.beginWrite()

let response = ["test":"test1"]

realm.create(BDRHome.self, value: response, update: true)

try realm.commitWrite()

} catch let error {print(error)}

}

}

}



class BDRHome:Object {

dynamic var id = "temporaryID"

dynamic var test:String?

override class func primaryKey() -> String? {

return "id"

}

}



*2015-12-18 10:50:15.366 RealmTest[63007:20816519] *** Terminating app due

to uncaught exception 'RLMException', reason: 'Can't set primary key
property 'id' to existing value 'temporaryID'.'*

**** First throw call stack:*

*(*

* 0 CoreFoundation 0x000000010f3fdf45
__exceptionPreprocess + 165*

* 1 libobjc.A.dylib 0x0000000111121deb
objc_exception_throw + 48*

* 2 Realm 0x000000010ef0f2e2 RLMDynamicSet
+ 4802*

* 3 Realm 0x000000010ef2782c
RLMCreateObjectInRealmWithValue + 2825*

* 4 RealmSwift 0x000000010ee8aa3b
_TFC10RealmSwift5Realm6createuRdq_CS_6Object_fS0_FTMq_5valuePSs9AnyObject_6updateSb_q_
+ 1659*

* 5 RealmTest 0x000000010edc741f
_TFC9RealmTest14ViewController11viewDidLoadfS0_FT_T_ + 767*

* 6 RealmTest 0x000000010edc7792
_TToFC9RealmTest14ViewController11viewDidLoadfS0_FT_T_ + 34*

* 7 UIKit 0x000000010fdaccc4
-[UIViewController loadViewIfRequired] + 1198*

* 8 UIKit 0x000000010fdad013
-[UIViewController view] + 27*

* 9 UIKit 0x000000010fc8651c -[UIWindow
addRootViewControllerViewIfPossible] + 61*

* 10 UIKit 0x000000010fc86c05 -[UIWindow
_setHidden:forced:] + 282*

* 11 UIKit 0x000000010fc984a5 -[UIWindow
makeKeyAndVisible] + 42*

* 12 UIKit 0x000000010fc12396
-[UIApplication
_callInitializationDelegatesForMainScene:transitionContext:] + 4131*

* 13 UIKit 0x000000010fc189c3
-[UIApplication _runWithMainScene:transitionContext:completion:] + 1750*

* 14 UIKit 0x000000010fc15ba3
-[UIApplication workspaceDidEndTransaction:] + 188*

* 15 FrontBoardServices 0x0000000113efa784
-[FBSSerialQueue _performNext] + 192*

* 16 FrontBoardServices 0x0000000113efaaf2
-[FBSSerialQueue _performNextFromRunLoopSource] + 45*

* 17 CoreFoundation 0x000000010f32a011
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17*

* 18 CoreFoundation 0x000000010f31ff3c
__CFRunLoopDoSources0 + 556*

* 19 CoreFoundation 0x000000010f31f3f3 __CFRunLoopRun
+ 867*

* 20 CoreFoundation 0x000000010f31ee08
CFRunLoopRunSpecific + 488*

* 21 UIKit 0x000000010fc154f5
-[UIApplication _run] + 402*

* 22 UIKit 0x000000010fc1a30d
UIApplicationMain + 171*

* 23 RealmTest 0x000000010edc93cd main + 109*

* 24 libdyld.dylib 0x0000000111d0892d start + 1*

* 25 ??? 0x0000000000000001 0x0 + 1*






but when I changed code as below



let response = ["test":"test1"]

realm.add(BDRHome(value:response), update:true)




then it does work!



what's the problem with create code





2015년 12월 17일 목요일 오후 8시 33분 38초 UTC+9, Henry Kim 님의 말:



On Thu, Dec 17, 2015 at 7:24 PM UTC, Marius Rackwitz <he...@realm.io> wrote:
Are you sure that the exception you're facing is originating from that particular line. Or do you have perhaps somewhere else some code around where you modify the id property of an already persisted object?
Can you show us where the variable response is coming from and some more context around that code and share the full stack trace?

--
Marius Rackwitz
iOS Product Engineer


On Thu, Dec 17, 2015 at 11:33 AM UTC, Henry Kim <he...@vcnc.co.kr> wrote:
Hi I'm using RealmSwift on my project.
but I faced the trouble when I create Object in a realm using same 'primary key'

here is my code then I don't know why the error kill my app even update field is true!

class BDRHomeView: Object {


......


let events = List<BDRHomeEvent>()

dynamic var id = "temporaryID"

override class func primaryKey() -> String? {

return "id"

}

}




...

realm.create(BDRHomeView.self, value:response, update:true)

....





here is the error :


*** Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'id' to existing value 'temporaryID'.'



could you help me? and I use Realm-Swift 0.96.3



回复全部
回复作者
转发
0 个新帖子