错误:[__ NSCFConstantString path]:发送无法识别的选择器

时间:2012-11-12 18:12:30

标签: ios core-data sync icloud managedobjectcontext

我正在尝试与 iCloud 同步核心数据

当我尝试 [self fetchedResultsControllerICloud] performFetch:& error]

- (ID)初始化

    [self managedObjectModelICloud];
    [self managedObjectContextICloud];

    if (![[self fetchedResultsControllerICloud] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

我有一个错误

 2012-11-12 23:50:34.917 My English words[1072:907] iCloud access at file://localhost/private/var/mobile/Library/Mobile%20Documents/LUB2V2L4R3~ru~________~My-English-words/
 2012-11-12 23:50:34.920 My English words[1072:907] count: 0
 2012-11-12 23:50:34.935 My English words[1072:110b] iCloudData: /private/var/mobile/Library/Mobile Documents/LUB2V2L4R3~ru~________~My-English-words/Documents/My_English_words.sqlite
 2012-11-12 23:50:34.938 My English words[1072:110b] -[__NSCFConstantString path]: unrecognized selector sent to instance 0x8ca28
 2012-11-12 23:50:34.940 My English words[1072:110b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString path]: unrecognized selector sent to instance 0x8ca28'
 *** First throw call stack:
 (0x331012a3 0x33fbf97f 0x33104e07 0x33103531 0x3305af68 0x344b840b 0x343805cf 0x3437e73d 0x7fe33 0x356f111f 0x356ff259 0x356ff3b9 0x3572fa11 0x3572f8a4)
 libc++abi.dylib: terminate called throwing an exception
 (lldb)
与之相关联 [_ persistentStoreCoordinatorICloud addPersistentStoreWithType:NSSQLiteStoreType string 在 - (NSPersistentStoreCoordinator *)persistentStoreCoordinatorICloud 方法

#define sqlLiteDataBaseName @"My_English_words.sqlite"
#define sqlLiteDataBasePath @"Documents"
#define iCloudEnabledAppID @"LUB2V2L4R3.ru.________.My-English-words"

//************************************************

- (NSManagedObjectContext *)managedObjectContextICloud
{
    if (_managedObjectContextICloud != nil) {
        return _managedObjectContextICloud;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinatorICloud];
    if (coordinator != nil) {
        _managedObjectContextICloud = [[NSManagedObjectContext alloc] init];
        [_managedObjectContextICloud setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContextICloud;
}
//************************************************

- (NSManagedObjectModel *)managedObjectModelICloud
{
    if (_managedObjectModelICloud != nil) {
        return _managedObjectModelICloud;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"My_English_words" withExtension:@"momd"];
    _managedObjectModelICloud = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModelICloud;
}
//************************************************
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinatorICloud
    {
        NSURL *iCloud = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        NSLog(@"iCloud access at %@", iCloud);
        NSString *pathString=[[iCloud path] stringByAppendingPathComponent:sqlLiteDataBasePath];
        _persistentStoreCoordinatorICloud = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModelICloud]];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        if([[NSFileManager defaultManager] fileExistsAtPath:pathString] == NO)
        {
            NSLog(@"!Exist");
            NSError *fileSystemError;
            [[NSFileManager defaultManager] createDirectoryAtPath:pathString
                                      withIntermediateDirectories:YES
                                                       attributes:nil
                                                            error:&fileSystemError];
            if(fileSystemError != nil) {
                NSLog(@"Error creating database directory %@", fileSystemError);
            }

        }

        NSString *iCloudData = [[[iCloud path] stringByAppendingPathComponent:sqlLiteDataBasePath] stringByAppendingPathComponent:sqlLiteDataBaseName];
        NSLog(@"iCloudData: %@",iCloudData);
        NSMutableDictionary *options = [NSMutableDictionary dictionary];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
        [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
        [options setObject:sqlLiteDataBasePath           forKey:NSPersistentStoreUbiquitousContentURLKey];


        [_persistentStoreCoordinatorICloud lock];

        [_persistentStoreCoordinatorICloud addPersistentStoreWithType:NSSQLiteStoreType
                                                        configuration:nil
                                                                  URL:[NSURL fileURLWithPath:iCloudData]
                                                              options:options
                                                                error:nil];

        [_persistentStoreCoordinatorICloud unlock];

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"asynchronously added persistent store!");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
        });
    });    
    return _persistentStoreCoordinatorICloud;
}
//************************************************
    - (NSFetchedResultsController *)fetchedResultsControllerICloud{

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"OriginalWords" inManagedObjectContext:_managedObjectContextICloud];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:1000];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                                  managedObjectContext:_managedObjectContextICloud
                                                                                                    sectionNameKeyPath:nil
                                                                                                             cacheName:nil];

    self.fetchedResultsControllerICloud  = theFetchedResultsController;
    _fetchedResultsControllerICloud.delegate = self;

    return _fetchedResultsControllerICloud;


}

1 个答案:

答案 0 :(得分:0)

错误已经解决。问题在于

[options setObject:sqlLiteDataBasePath forKey:NSPersistentStoreUbiquitousContentURLKey];

其中 sqlLiteDataBasePath 路径应该已满

[[iCloud URLByAppendingPathComponent:sqlLiteDataBasePath] URLByAppendingPathComponent:sqlLiteDataBaseName]

非常愚蠢的错误,但现在又出现了另一个错误:

'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation'. 

我认为这是因为

[_managedObjectContextICloud save:&error]