到目前爲止,我使用的是共享的Coredata實例。在視圖控制器中,我會呼叫[[Storage instance] managedObjectContext]
在視圖控制器中傳遞!Coredata managedobjectContext和私有ivars的共享實例
我說你可能很殘酷嗎?請告訴我爲什麼。
我注意到在大多數例子中,viewControllers在@private頭文件中有managedObjectContexts。我們爲什麼需要私人電話?
最後,我們聲明像
NSManagedObjectContext *_managedObjectContext;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
然後在viewController.m
@synthesize managedObjectContext = _managedObjectContext;
什麼 「_」 managedObjectContext的意義。爲什麼是下劃線?
最後,這些都會影響iCloud?在一個教程作家說,
高德應該是私有的,這是非常重要的,所有的代碼 總是通過存取方法去確保這些 正確初始化。如果沒有有趣的話,KVC可能會「幫助」我們 太多。隨着iCloud的異步導入數據,有更多的 定時和多線程問題
沒有搞笑_ ...請解釋WATS回事..
@interface LStorage : NSObject {
@private
NSString *identifier;
NSManagedObjectContext *managedObjectContext;
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
+ (LStorage *) instance;
in LStorage.m
+ (LStorage *) instance {
@synchronized(self) {
if (instance == nil) {
instance = [[LStorage alloc] initWithIdentifier:kIdentifier];
}
}
return instance;
}
//identifier is just used to name the mom model filename.
- (id) initWithIdentifier:(NSString *)anIdentifier {
self = [super init];
if(self != nil) {
identifier = anIdentifier;
}
return self;
}
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
// Make life easier by adopting the new NSManagedObjectContext concurrency API
// the NSMainQueueConcurrencyType is good for interacting with views and controllers since
// they are all bound to the main thread anyway
if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc performBlockAndWait:^{
// even the post initialization needs to be done within the Block
[moc setPersistentStoreCoordinator: coordinator];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
}];
managedObjectContext = moc;
}else{
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
}
return managedObjectContext;
}
我與初始化 – jasonIM 2012-02-20 23:52:24
更新想烏爾評論 – jasonIM 2012-02-20 23:52:36
您初始化是好的,我更感興趣的是你的'managedObjectContext'方法在那個類中。 – Rog 2012-02-21 00:03:56