SRCoreDataStack 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行
pod "SRCoreDataStack"
要运行示例项目,请克隆仓库,并首先从示例目录运行 pod install
获取默认共享实例
self.dataStack = [SRCoreDataStack defaultStackForDataModel:@"Example"];
将来自网络调用的对象保存到 Core Data
[self.dataStack saveObjects:wireObjects inEntity:@"Movie" withWireAttribute:@"id" andLocalAttribute:@"movie_id" andConfiguration:^NSManagedObject *(NSDictionary *obj, NSManagedObject *mo, NSManagedObjectContext *currentCtx) {
Movie *movie = (Movie*)mo;
movie.movie_id = obj[@"id"];
movie.movie_title = obj[@"title"];
movie.movie_description = obj[@"description"];
// define its parent-child relationship
NSMutableArray *ma = [NSMutableArray array];
for (NSString *genreString in obj[@"genres"])
{
MovieGenre *movieGenre = [MovieGenre insertNewObjectIntoContext:currentCtx];
movieGenre.genre_name = genreString;
[ma addObject:movieGenre];
}
movie.movie_genres = [NSSet setWithArray:[NSArray arrayWithArray:ma]];
return movie;
}];
示例项目使用了 这个后端项目。一旦从终端下载并运行,可以通过浏览器查看后端项目。然后可以在应用之间实现无缝实时同步。
SRCoreDataStack 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。