RestKit:无法使用coredata执行映射 [英] RestKit : Not able to perform mapping using coredata

查看:156
本文介绍了RestKit:无法使用coredata执行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用休息套件0.20.3和Xcode 5.没有核心数据我可以执行所有休息套件操作,但是当我尝试与核心数据,我甚至不能执行 GET 由于一些问题。我不能想出来。我是核心数据的新用户。所以pls帮助。这是我的代码:

I'm using rest kit 0.20.3 and Xcode 5. Without core data I'm able to perform all rest kit operation, but when I've tried it with core data, I'm not even able to perform GET due to some problem. I can't figure it out. I'm new with core data. So pls help. Here is my code:

AppDelegate.m

AppDelegate.m

@implementation CardGameAppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RKLogConfigureByName("RestKit", RKLogLevelWarning);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.3:3010/"]];


    RKManagedObjectStore *objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.managedObjectModel];

    objectManager.managedObjectStore = objectStore;

    RKEntityMapping *playerMapping = [RKEntityMapping mappingForEntityForName:@"Player" inManagedObjectStore:objectStore];
    [playerMapping addAttributeMappingsFromDictionary:@{@"id": @"playerId",
                                                        @"name": @"playerName",
                                                        @"age" : @"playerAge",
                                                        @"created_at": @"createdAt",
                                                        @"updated_at": @"updatedAt"}];



    RKResponseDescriptor *responseDesc = [RKResponseDescriptor responseDescriptorWithMapping:playerMapping method:RKRequestMethodGET pathPattern:@"/players.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:responseDesc];

    PlayersTableViewController *ptvc = (PlayersTableViewController *)self.window.rootViewController;
    ptvc.managedObjectContext = self.managedObjectContext;

    return YES;
}

和playerTableViewController.h的代码

and code for playerTableViewController.h

#import <UIKit/UIKit.h>

@interface PlayersTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@end

和PlayerTableViewController.m get方法:

and PlayerTableViewController.m get method:

-(void)loadPlayers{
    [[RKObjectManager sharedManager] getObjectsAtPath:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
        [self.refreshControl endRefreshing];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        [self.refreshControl endRefreshing];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }];
}

我收到以下错误:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = http://192.168.1.3:3010/players.json)'


推荐答案

只需创建 objectStore 是不够的,您需要完成Core Data堆栈设置的其余部分。您还应该在RestKit中执行此操作,而不是在应用程序委托(这是默认的Apple提供的配置)。这将是一些符合您的要求(为您的要求):

It isn't enough to just create the objectStore, you need to complete the rest of the Core Data stack setup. You should also do this in RestKit, not in the app delegate (which is the default Apple provided configuration). This will be something along the lines of (tailor for your requirements):

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[managedObjectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XXXX.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath
                                                                 fromSeedDatabaseAtPath:nil
                                                                      withConfiguration:nil
                                                                                options:@{
                                                                                          NSMigratePersistentStoresAutomaticallyOption : @(YES),
                                                                                          NSInferMappingModelAutomaticallyOption : @(YES),
                                                                                              }
                                                                                  error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

这篇关于RestKit:无法使用coredata执行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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