this is a sample code that works, to be strict, you might have to implement some other mechanism:
1. Add a clock or a maxTimes to restrict the iteration.
Check if managedObjectContext is nil before the first time you use it, for YES, check the UIDocumentState as:
More ***UIDocumentState***s should be considered, this includes:
UIDocumentStateClosed (haven’t done the open/create yet, just wait for a moment and try again)
UIDocumentStateSavingError (success be NO in completion handler)
UIDocumentStateEditingDisabled (temprorary situation, try again)
UIDocumentStateInConflict (e.g., because some other deviece changed it via iCloud)
//header file
@interface file2CoredataClass : NSObject{
}
-(void)manualSave;
-(void)manualClose;
-(void)addAllPaths;
@property NSManagedObjectContext *managedObjectContext;
@end
//implementation
@implementation file2CoredataClass
UIManagedDocument *doc;
NSFileManager *fm;
-(id)init{
self=[super init];
if(self){
fm = [NSFileManager defaultManager];
NSURL *docDirectory = [[fm URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask] firstObject];
//notice:for some reason i don't know, the directory may not exist, and you can never create a document in this situation.
//so, check if directory exists, and if not, create the directory.
if(![fm fileExistsAtPath:[NSString stringWithFormat:@"%@",docDirectory] isDirectory:&isDir]){
[fm createDirectoryAtPath:[NSString stringWithFormat:@"%@",docDirectory] withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *docName = @"geoDoc";
NSURL *url = [docDirectory URLByAppendingPathComponent:docName];
doc = [[UIManagedDocument alloc]initWithFileURL:url]; // this creates the UIManagedDocument but not yet open or create the underlying file.
//open or create the file
[self openCreateFile:fm documentURL:url];
}
return self;
}
-(void)openCreateFile:(NSFileManager *)fm documentURL:(NSURL *)url{
BOOL fileExists = [fm fileExistsAtPath:[url path]];
if(fileExists){
[doc openWithCompletionHandler:^(BOOL success) {
//block to execute as file operation is asynchronous
if(!success){
[self openCreateFile:fm documentURL:url];
}else if(doc.documentState == UIDocumentStateNormal){
_managedObjectContext = doc.managedObjectContext;
}
}];
}else{
[doc saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
//block to execute as file operation is asynchronous
if(!success){
[self openCreateFile:fm documentURL:url];
}else if(doc.documentState == UIDocumentStateNormal){
_managedObjectContext = doc.managedObjectContext;
}
}];
}
}
@end