【问题标题】:I'm not getting the NSPersistentStoreDidImportUbiquitousContentChangesNotification (with code sample)我没有收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification (带有代码示例)
【发布时间】:2011-12-07 18:51:41
【问题描述】:

我正在尝试学习如何将 iCloud 与 coredata 一起使用。我的目标是在 mac/ios 上同步单个数据库文件。我的 mac 和 iOS 应用程序似乎都在更新 iCloud,因为我可以在 System Preferences->iCloud->Manage 下看到它......有一个名为 Unknown 的应用程序,每次我在 ios/mac 应用程序上更改某些内容时,它都会变得越来越大。所以这让我觉得这些应用程序正在将更改存储到云中。问题是,当我在另一个平台上更改某些内容时,我没有收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification

这是我的代码(mac 应用程序):

- (void)persistentStoreDidImportUbiquitousContentChangesNotification:(NSNotification *)notif
{
    NSLog(@"%@", notif);
}

- (NSURL *)applicationFilesDirectory
{
    NSString *identifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *libraryURL = [[[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:identifier];

    if(![fileManager fileExistsAtPath:[libraryURL path]])
    {
        [fileManager createDirectoryAtPath:[libraryURL path] withIntermediateDirectories:YES attributes:nil error:nil];
    }

    return libraryURL;
}

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel)
    {
        return __managedObjectModel;
    }

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return __managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator)
    {
        return __persistentStoreCoordinator;
    }

    NSManagedObjectModel *mom = [self managedObjectModel];
    if (!mom)
    {
        NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
        return nil;
    }

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
    NSError *error = nil;

    NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] error:&error];

    if (!properties)
    {
        BOOL ok = NO;
        if ([error code] == NSFileReadNoSuchFileError)
        {
            ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
        }
        if (!ok)
        {
            [[NSApplication sharedApplication] presentError:error];
            return nil;
        }
    }
    else
    {
        if ([[properties objectForKey:NSURLIsDirectoryKey] boolValue] != YES)
        {
            NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]]; 

            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
            error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];

            [[NSApplication sharedApplication] presentError:error];
            return nil;
        }
    }

    NSString *appBundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSURL *storeURL = [applicationFilesDirectory URLByAppendingPathComponent:@"MyApp.sqlite"];
    NSURL *cloudURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Database"];

    NSPersistentStoreCoordinator *coordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease];
    NSDictionary *optionsDict = [NSDictionary dictionaryWithObjectsAndKeys:appBundleID, NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
    if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDict error:&error])
    {
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
    __persistentStoreCoordinator = [coordinator retain];

    return __persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator)
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
        [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
        NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
    __managedObjectContext = [[NSManagedObjectContext alloc] init];
    [__managedObjectContext setPersistentStoreCoordinator:coordinator];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreDidImportUbiquitousContentChangesNotification:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];

    return __managedObjectContext;
}

【问题讨论】:

  • 我发现这些文件在云端:“.baseline”、“.cdmetadata.nosync”、“kukosk.F8533032-7CA2-5A89-8097-62CD3D1C2FA3”、“mobile.CB9C2446-D756” -575E-A553-7ABCFD2D369F"

标签: cocoa core-data xcode4.2 icloud


【解决方案1】:

我在 iOS 和 MacOS X 上运行 CoreData 堆栈时遇到了同样的问题。在我的 iPhone 和 iPad 之间,NSPersistentStoreDidImportUbiquitousContentChangesNotification:s 运行良好。

在 Mac 上,我最终定期执行 NSFetchRequest(比如每 5 秒)并丢弃了结果。通知才到。

【讨论】:

    【解决方案2】:

    我在使用 iOS SDK 时遇到了同样的问题。

    要获得“NSPersistentStoreDidImportUbiquitousContentChangesNotification”通知,您必须从 NSNotification 中心调用“- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject”。

    我添加了这一行

    [[NSNotificationCenter defaultCenter] addObserver:masterViewController selector:@selector(notifyiCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:[self persistentStoreCoordinator]];

    在 AppDelegate 的“didFinishLaunchingWithOptions”方法中。 在我的情况下,(iOS5)它运行良好,但我不知道它是否也可以在 mac os 中运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多