我在我的應用程序中實現了類似的事情,因爲我想將更新推入一組特定的數據加載我從2個數據庫中的數據。
我已經做到了,使用1個對象模型,1個管理對象上下文,但是2個持久性存儲,每一個加載不同的源碼文件的方式。爲此,我在數據模型文件中創建了2個配置,然後將相應的實體拖放到每個配置中。然後當你創建持久性商店這樣做:
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Configuration1" URL:store1URL options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil] error:&error];
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Configuration2" URL:store2URL options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil] error:&error];
我發現迄今爲止唯一的問題是你不能有商店之間的關係。 This answer顯示如何使用fetched屬性複製關係。
希望這會有所幫助。
來源
2013-01-25 14:20:14
JDx
感謝您的和其他答案的鏈接。我一直在考慮兩個獨立的模型,但我想我知道你正在使用一個模型(這可能是從每個持久性存儲裝載某些實體)所做的。然而,它並沒有回答在兩個不同的上下文中單個上下文實現是否具有性能優勢的問題。對此有何想法? – SimonTheDiver