RestKit 0.20 — 创建新 NSManagedObject 的首选方法是什么? [英] RestKit 0.20 — What is the preferred way to create a new NSManagedObject?

查看:43
本文介绍了RestKit 0.20 — 创建新 NSManagedObject 的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道在 RestKit 0.20 中创建新 NSManagedObject 的最佳方法是什么?目前我的代码看起来像这样:

#pragma mark - 导航按钮- (void)createButtonDidTouch{//创建新专辑对象NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];NSManagedObjectContext *parentContext = RKObjectManager.sharedManager.managedObjectStore.mainQueueManagedObjectContext;context.parentContext = parentContext;NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:parentContext];专辑 *newAlbum = [[专辑分配] initWithEntity:entityDescription insertIntoManagedObjectContext:context];//传递对象来创建要操作的视图AlbumCreateViewController *createViewController = [[AlbumCreateViewController alloc] initWithData:newAlbum];createViewController.delegate = self;createViewController.managedObjectContext = 上下文;UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:createViewController];navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;[self presentViewController:navController 动画:YES 完成:nil];}#pragma mark - 创建视图控制器委托- (void)createViewControllerDidSave:(NSManagedObject *)data{//关闭创建视图控制器和 POST//FIXME:添加restkit代码以保存对象NSLog(@"保存对象...");NSDictionary *userInfo = [KeychainUtility load:@"userInfo"];NSString *path = [NSString stringWithFormat:@"/albums/add/%@/%@", userInfo[@"userID"], userInfo[@"apiKey"]];[RKObjectManager.sharedManager postObject:data path:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {operation.targetObject = 数据;} 失败:^(RKObjectRequestOperation *operation, NSError *error) {NSLog(@"创建相册错误:%@", error);}];[自我解雇ViewControllerAnimated:是完成:nil];}- (void)createViewControllerDidCancel:(NSManagedObject *)data{//关闭创建视图控制器NSLog(@"删除对象...");//FIXME:添加restkit代码以删除对象[自我解雇ViewControllerAnimated:是完成:nil];}

我也很想知道我的职责是保存/删除这个对象.如果我通过 RestKit POST 到服务器,是否保存了托管对象上下文?

如果我决定取消这个创建过程怎么办——删除这个对象的首选方法是什么?

基本上,RestKit 为我做了多少,我应该确保我在做什么.我还没有找到太多关于此的文档,我想说清楚.

解决方案

当你为给定的对象初始化一个 RKManagedObjectRequestOperation 时,RestKit 会为那个对象获取一个永久的对象 ID,然后创建一个子托管对象对象上下文,其父上下文是对象插入的上下文.该操作然后执行 HTTP 请求以完成并获得响应.

如果响应成功并且响应的映射成功(注意映射发生在这个私有子上下文中),则保存私有子上下文.调用的保存类型由 savesToPersistentStore 属性的值决定(参见 http://restkit.org/api/0.20.0/Classes/RKManagedObjectRequestOperation.html#//api/name/savesToPersistentStore).

YES 时,上下文通过NSManagedObjectContext 类别方法saveToPersistentStore 递归保存到持久存储区(见http://restkit.org/api/0.20.0/Categories/NSManagedObjectContext+RKAdditions.html).

NO 时,上下文通过普通的 [NSManagedObjectContext save:] 消息保存,该消息将更改推送"回父上下文.在您将它们保存回来之前,它们将保持在该上下文的本地.请记住,托管对象上下文父/子层次结构可以与您在应用程序中创建的时间一样长.

如果 HTTP 请求失败或映射过程中出现错误,则不保存私有上下文并认为操作失败.这意味着不会将任何更改保存回原始 MOC,让您的对象图与操作开始之前一样(除了正在发送的对象,如果是临时的,现在具有永久对象 ID 但仍未保存).

I'm curious to know what the best way is to create a new NSManagedObject in RestKit 0.20? Currently my code looks something like this:

#pragma mark - navigation buttons

- (void)createButtonDidTouch
{
    // create new album object    
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    NSManagedObjectContext *parentContext = RKObjectManager.sharedManager.managedObjectStore.mainQueueManagedObjectContext;
    context.parentContext = parentContext;
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:parentContext];
    Album *newAlbum = [[Album alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:context];

    // pass object to create view to manipulate
    AlbumCreateViewController *createViewController = [[AlbumCreateViewController alloc] initWithData:newAlbum];
    createViewController.delegate = self;
    createViewController.managedObjectContext = context;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:createViewController];
    navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [self presentViewController:navController animated:YES completion:nil];
}

#pragma mark - create view controller delegate

- (void)createViewControllerDidSave:(NSManagedObject *)data
{
    // dismiss the create view controller and POST

    // FIXME: add restkit code to save the object
    NSLog(@"save the object...");

    NSDictionary *userInfo = [KeychainUtility load:@"userInfo"];
    NSString *path = [NSString stringWithFormat:@"/albums/add/%@/%@", userInfo[@"userID"], userInfo[@"apiKey"]];

    [RKObjectManager.sharedManager postObject:data path:path parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        operation.targetObject = data;
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"create album error: %@", error);
    }];

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createViewControllerDidCancel:(NSManagedObject *)data
{
    // dismiss the create view controller

    NSLog(@"delete the object...");
    // FIXME: add restkit code to delete the object

    [self dismissViewControllerAnimated:YES completion:nil];
}

I'm also curious to know what my responsibilities are for saving / deleting this object. If I POST to the server via RestKit is the managed object context saved?

What if I decide to cancel this creation process — what's the preferred way to delete this object?

Basically how much is RestKit doing for me, and what should I make sure I'm doing. I haven't found much documentation on this and would like to be clear on it.

解决方案

When you initialize an RKManagedObjectRequestOperation for a given object, RestKit will obtain a permanent object ID for that object and then create a child managed object context whose parent context is the context the object is inserted into. The operation then executes the HTTP request to completion and obtains a response.

If the response is successful and the mapping of the response is successful (note that the mapping occurs within this private child context), then the private child context is saved. The type of save invoked is determined by the value of the savesToPersistentStore property (see http://restkit.org/api/0.20.0/Classes/RKManagedObjectRequestOperation.html#//api/name/savesToPersistentStore).

When YES, the context is saved recursively all the way back to the persistent store via the NSManagedObjectContext category method saveToPersistentStore (see http://restkit.org/api/0.20.0/Categories/NSManagedObjectContext+RKAdditions.html).

When NO, the context is saved via a vanilla [NSManagedObjectContext save:] message, which 'pushes' the changes back to the parent context. They will remain local to that context until you save them back. Keep in mind that managed object context parent/child hierarchies can be as long as you create within the application.

If the HTTP request failed or there was an error during the mapping process, the private context is not saved and the operation is considered failed. This means that no changes are saved back to the original MOC, leaving your object graph just as it was before the operation was started (except the object being sent, if temporary, now has a permanent object ID but is still unsaved).

这篇关于RestKit 0.20 — 创建新 NSManagedObject 的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆