{"action":"created","issue":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/611","labels_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/611/labels{/name}","comments_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/611/comments","events_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/611/events","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/611","id":59121757,"number":611,"title":"crash when calling CBLModel's initWithNewDocumentInDatabase","user":{"login":"hermanccw","id":1000084,"avatar_url":"https://avatars.githubusercontent.com/u/1000084?v=3","gravatar_id":"","url":"https://api.github.com/users/hermanccw","html_url":"https://github.com/hermanccw","followers_url":"https://api.github.com/users/hermanccw/followers","following_url":"https://api.github.com/users/hermanccw/following{/other_user}","gists_url":"https://api.github.com/users/hermanccw/gists{/gist_id}","starred_url":"https://api.github.com/users/hermanccw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hermanccw/subscriptions","organizations_url":"https://api.github.com/users/hermanccw/orgs","repos_url":"https://api.github.com/users/hermanccw/repos","events_url":"https://api.github.com/users/hermanccw/events{/privacy}","received_events_url":"https://api.github.com/users/hermanccw/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-26T18:55:03Z","updated_at":"2015-02-26T20:43:23Z","closed_at":"2015-02-26T20:43:23Z","body":"Hi all,\r\n\r\nAnother crash in production:\r\n\r\n```\r\nThread : Fatal Exception: NSInternalInconsistencyException \"Document already has a model\"\r\n0  CoreFoundation                 0x2bab7e3f __exceptionPreprocess + 126\r\n1  libobjc.A.dylib                0x39193c8b objc_exception_throw + 38\r\n2  CoreFoundation                 0x2bab7d15 +[NSException raise:format:]\r\n3  Foundation                     0x2c7885af -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 90\r\n4  myApp                       0x00682303 CBLVersion\r\n5  myApp                       0x00682401 CBLVersion\r\n6  myApp                       0x00681ddd CBLVersion\r\n7  myApp                       0x001c9d55 -[myAppUserFlag initWithNewDocumentInDatabase:] (myAppUserFlag.m:18)\r\n8  myApp                       0x001d0413 -[myAppGroup createUserFlag] (HubGroup.m:462)\r\n9  myApp                       0x001d19c1 -[myAppGroup markGracePeriodWarning3Shown] (myAppGroup.m:748)\r\n10 myApp                       0x000c201b -[myAppViewController tryShowGracePeriodWarning] (myAppViewController.m:1167)\r\n11 myApp                       0x000b7f57 __50-[myAppViewController handleProductsReceived:]_block_invoke (myAppViewController.m:327)\r\n12 myApp                       0x0058384f __80-[NSUserDefaults(GroundControl) registerDefaultsWithURLRequest:success:failure:]_block_invoke (NSUserDefaults+GroundControl.m:80)\r\n13 libdispatch.dylib              0x396f38cb _dispatch_call_block_and_release + 10\r\n14 libdispatch.dylib              0x396f38b7 _dispatch_client_callout + 22\r\n15 libdispatch.dylib              0x396f70bf _dispatch_main_queue_callback_4CF + 722\r\n16 CoreFoundation                 0x2ba7dbe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8\r\n17 CoreFoundation                 0x2ba7c2e9 __CFRunLoopRun + 1512\r\n18 CoreFoundation                 0x2b9ca621 CFRunLoopRunSpecific + 476\r\n19 CoreFoundation                 0x2b9ca433 CFRunLoopRunInMode + 106\r\n20 GraphicsServices               0x32d390a9 GSEventRunModal + 136\r\n21 UIKit                          0x2efb5359 UIApplicationMain + 1440\r\n22 myApp                       0x000a543b main (main.m:14)\r\n23 libdyld.dylib                  0x39713aaf start + 2\r\n```\r\n\r\nThis happens when I try to call initWithNewDocumentInDatabase (https://github.com/couchbase/couchbase-lite-ios/blob/release/1.0.4-hotfix1/Source/API/CBLModel.m#L53-L60) .  It seems like that it think there is a document there already where initWithNewDocumentInDatabase pass in nil when calling initWithDocument.\r\n\r\nCould this be a multi-threading bug?  Note that before calling initWithNewDocumentInDatabase in my code, I did call the following method to make sure there is no such doc in my code.\r\n\r\n```\r\n- (myAppUserFlag*) userFlag {\r\nCBLDatabase *database = [[myAppTouchDBServiceManager sharedInstance] databaseForGroupId:self.document.documentID];\r\n    CBLDocument *userFlagDoc = [database existingDocumentWithID:[myAppUserFlag idForModel]];\r\n    myAppUserFlag *userFlag;\r\n    if (userFlagDoc) {\r\n        userFlag = [myAppUserFlag modelForDocument:userFlagDoc];\r\n    }\r\n    return userFlag;\r\n}\r\n```\r\nso I will only call initWithNewDocumentInDatabase if userFlag returns nil\r\n\r\nthanks,\r\nHerman"},"comment":{"url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/comments/76267423","html_url":"https://github.com/couchbase/couchbase-lite-ios/issues/611#issuecomment-76267423","issue_url":"https://api.github.com/repos/couchbase/couchbase-lite-ios/issues/611","id":76267423,"user":{"login":"hermanccw","id":1000084,"avatar_url":"https://avatars.githubusercontent.com/u/1000084?v=3","gravatar_id":"","url":"https://api.github.com/users/hermanccw","html_url":"https://github.com/hermanccw","followers_url":"https://api.github.com/users/hermanccw/followers","following_url":"https://api.github.com/users/hermanccw/following{/other_user}","gists_url":"https://api.github.com/users/hermanccw/gists{/gist_id}","starred_url":"https://api.github.com/users/hermanccw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hermanccw/subscriptions","organizations_url":"https://api.github.com/users/hermanccw/orgs","repos_url":"https://api.github.com/users/hermanccw/repos","events_url":"https://api.github.com/users/hermanccw/events{/privacy}","received_events_url":"https://api.github.com/users/hermanccw/received_events","type":"User","site_admin":false},"created_at":"2015-02-26T20:43:23Z","updated_at":"2015-02-26T20:43:23Z","body":"Alright, I'll try. thanks for looking into this."}}