I have a feature in my app whereby the User can reset everything on the app by click of a button. At this point, instead of trying to delete all the Core Data relations (Cascade Delete) and other complications, I decided to actually remove the entire UIManagedDocument using this piece of code
-(void)cleanUpDocument
{
[[NSFileManager defaultManager] removeItemAtPath:[self.document.fileURL path] error:nil];
}
This should remove the Document I assume? But it sometimes throws an error. And the weird part is that, when I try to re-create the Document the next time, I get an error saying "Can't create File, File already Exists". The code that i use to create the Document is this :-
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) {
[self.document saveToURL:self.document.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:nil]
}
My question is this :- what is the best/correct way to remove/delete an entire UIManagedDocument and start fresh on next successful login?
Thanks in advance.