【发布时间】:2013-06-14 20:55:44
【问题描述】:
我有一个 OS X 应用程序通过一个普遍存在的容器来回同步单个文档到一个 iOS 等效应用程序。 iOS 应用程序在 Mac 端发生变化时接收数据,并在 iOS 端发生变化时发送数据(因此 iOS 应用程序正在运行),Mac 应用程序在 Mac 端发生变化时发送数据,并且在启动应用程序时接收数据,但它似乎在运行时没有再次检查任何数据。我希望它能够立即自动更新任何更改,就像 OS X“Notes”应用程序根据 iOS 端的更改所做的那样。
在启动时,这是被调用的相关函数:
+(NSMutableDictionary *)getAllNotes {
if(allNotes == nil) {
allNotes = [[NSMutableDictionary alloc]initWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:kAllNotes]];
cloudDoc = [[CloudDocument alloc]initWithContentsOfURL:[self notesURL] ofType:NSPlainTextDocumentType error:nil];
[cloudDoc saveToURL:[self notesURL] ofType:NSPlainTextDocumentType forSaveOperation:NSSaveOperation error:nil];
}
return allNotes;
}
“CloudDocument”类(NSDocument 的子类)包括:
#import "Data.h"
@implementation CloudDocument
-(NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [NSKeyedArchiver archivedDataWithRootObject:[Data getAllNotes]];
}
-(BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
NSDictionary *dict = (NSDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
[Data didReceiveCloudData:dict];
return YES;
}
+(BOOL)autosavesInPlace {
return YES;
}
@end
这又回到了:
+(void)didReceiveCloudData:(NSDictionary *)d {
allNotes = [[NSMutableDictionary alloc]initWithDictionary:d];
[[NSUserDefaults standardUserDefaults] setObject:allNotes forKey:kAllNotes];
[cloudDoc updateChangeCount:NSChangeDone];
}
我认为问题在于我的代码中没有任何部分相当于“定期检查无处不在的容器是否已更改,然后执行...”等短语。我是肯定有一个众所周知的过程(NSDocument 中的一些通知事件或其他东西),但我已经四处搜索,我发现的所有东西都是针对 iOS/UIDocuments 而不是 OS X/NSDocuments,或者这都是理论和结束我的头没有任何有形的代码示例来梳理和挑选。
谁能帮我注册一个 iCloud 文档在 ubiquity 容器中发生变化的方法,最好把它放在哪里(AppDelegate、CloudDocument.m 等)?我只有一个文件在同步,由常量kAllNotes 表示,所以我不需要跟踪一堆不同的文件或任何东西。我很确定我可以使用在启动时运行的代码来做需要做的事情,我只是不知道该怎么做才能启动自动同步过程。
提前谢谢你!
PS 我还是个初学者,所以非常感谢教程和代码示例。
【问题讨论】: