UIManagedDocument used in UITabViewController crashes when trying open an existing database -
i using justin driscoll's article on core data uimanageddocument in singleton pattern set uitabviewcontroller. running app on simulator. working fine first time. database created , can see data in tableview controller each tab. when restart application, app crashes error
assertion failure in -[uimanageddocument openwithcompletionhandler:], ** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'attempt open or revert document has open or revert operation in flight
my code causes crash. did debugging using nslog statements.
if (![[nsfilemanager defaultmanager] fileexistsatpath:[self.document.fileurl path]]) { nslog(@"document doesnot exist , hence start creating"); [self dataintodocument:self.document]; nslog(@"document finished creating"); [self.document savetourl:self.document.fileurl forsaveoperation:uidocumentsaveforcreating completionhandler:ondocumentdidload]; nslog(@"saved url on disk"); } else if (self.document.documentstate == uidocumentstateclosed) { nslog(@"document closed , needs opened"); [self.document openwithcompletionhandler:ondocumentdidload]; } else if (self.document.documentstate == uidocumentstatenormal) { nslog(@"test document in normal state"); ondocumentdidload(yes); }
result first run when database doesn't exists
2013-08-12 14:51:35.458 <my app>[368:11603] document doesnot exist , hence created start 2013-08-12 14:51:38.716 <my app>[368:11603] document doesnot exist , hence finished creating 2013-08-12 14:51:38.718 <my app>[368:11603] nsmanagedcontext did save. 2013-08-12 14:51:38.718 <my app>[368:11603] saved url on disk 2013-08-12 14:51:38.721 <my app>[368:11603] document closed , needs opened 2013-08-12 14:51:38.772 <my app>[368:11603] nsmanagedobjects did change
result second run: when database exists @ url
2013-08-12 14:53:43.758 <my app> [380:11603] document closed , needs opened 2013-08-12 14:53:43.761 <my app>[380:11603] document closed , needs opened 2013-08-12 14:53:43.762
i understand reason why failing, not supposed open in close succession. have same code in view controllers control tabs instance of uimanageddocument. please let me know missing.
-(id) initwithcoder:(nscoder *)adecoder { self = [super initwithcoder:adecoder]; if (!self.databasedocument) { [[ltdatabasedocumenthandler shareddatabasedocumenthandler] performwithdocument:^ (uimanageddocument *document) { self.databasedocument = document; [self populatetableviewarrayfromdocument:self.databasedocument]; }]; } return self; }
i figured. need use -(void)viewwillappear:animated method instantiate shared instance of uimanageddocument instead of -(id)initwithcoder. viewwillappear method gets executed when user tries view tab.
Comments
Post a Comment