gpt4 book ai didi

cocoa - 我没有收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification (带有代码示例)

转载 作者:行者123 更新时间:2023-12-03 16:09:45 25 4
gpt4 key购买 nike

我正在尝试学习如何将 iCloud 与 coredata 结合使用。我的目标是在 mac/ios 上同步单个数据库文件。我的 mac 和 iOS 应用程序似乎都在更新 iCloud,因为我可以在“系统偏好设置”->“iCloud”->“管理”下看到它...有一个名为“未知”的应用程序,每次我在 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;
}

最佳答案

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

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

我添加了这一行

[[NSNotificationCenter defaultCenter] addObserver:masterViewController 选择器:@selector(notifyiCloud:) 名称:NSPersistentStoreDidImportUbiquitousContentChangesNotification 对象:[self permanentStoreCoordinator]];

在 AppDelegate 的“didFinishLaunchingWithOptions”方法中。就我而言,(iOS5)它运行得很好,但我不知道它是否也能在 mac os 中运行。

关于cocoa - 我没有收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification (带有代码示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7768528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com