我有一个singleton delayed-transaction管理器类,它负责排队和离队由于各种因素(网络可达性,邻近度等)而无法完成的iphone到应用服务器事务。当可恢复性/邻近性恢复时,它会尝试使事务出队。另一个事务也可能在出队正在发生的同时被排队。
我的理解是,NSUserDefaults是一个可变字典SO link线程安全的包装,这正是我想要的。同样的SO链接认为NSUserDefaults适用于用户首选项。同步NSKeyedArchiver或NSUserDefaults
这些交易不是用户的喜好,但这是我想要的功能。 所以,这里的问题是,我可以在不损坏字典的情况下读/写/同步NSUserDefaults中的事务的NSMutableDictionary,还是需要通过NSKeyed(Un)Archiver(见下文)读写来实现自己的锁定机制?
一些代码:
- (void) queThing:(Thing *)aThing {
// @synchronized this block?
NSMutableDictionary * QD = [self readFile];
[QD setObject:aThing forKey:aThing.thingId];
[self writeFile: QD];
// @ sync?
}
- (void) writeFile:(NSMutableDictionary *)theData {
BOOL status = [NSKeyedArchiver archiveRootObject: theData toFile:archivePath];
if (!status) {
DebugLog(@"Write to archive failed.");
} else {
DebugLog(@"Write to archive SUCCEEDED.");
}
}
- (NSMutableDictionary *) readFile {
NSMutableDictionary *Q =[NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
if (! Q) {
Q = [NSMutableDictionary dictionaryWithCapacity:2];
}
return Q;
}
出列:
// @synchronized this block?
NSMutableDictionary * QD = [self readFile];
[QD removeObjectForKey:thing.thingId];
[self writeFile: QD];
// @ sync?
谢谢。我想我会去读/写锁。 – Rayfleck 2011-05-25 14:11:12