Restkit“*** -[RKManagedObjectMapping initWithEntity:] 中的断言失败”

Posted

技术标签:

【中文标题】Restkit“*** -[RKManagedObjectMapping initWithEntity:] 中的断言失败”【英文标题】:Restkit "*** Assertion failure in -[RKManagedObjectMapping initWithEntity:]" 【发布时间】:2011-11-09 13:24:40 【问题描述】:

在初始化 Restkit 时,我在运行时遇到此断言失败:

2011-11-09 08:00:50.817 myapp[32572:207] application:didFinishLaunchingWithOptions:
2011-11-09 08:00:50.820 myapp[32572:207] D app:myappAppDelegate.m:179 Initializing Restkit...
2011-11-09 08:00:50.889 myapp[32572:207] D restkit.network.queue:RKRequestQueue.m:214 Queue <RKRequestQueue: 0x742bde0> has been unsuspended
2011-11-09 08:00:50.889 myapp[32572:207] D restkit.network.queue:RKRequestQueue.m:48 Shared queue initialized: <RKRequestQueue: 0x742bde0>
2011-11-09 08:00:50.890 myapp[32572:207] D restkit.network.queue:RKRequestQueue.m:207 Queue <RKRequestQueue: 0x742bde0> has been suspended
2011-11-09 08:00:50.890 myapp[32572:207] D restkit.network:RKClient.m:250 Base URL changed for client <RKClient: 0x7136a00>, suspending main queue until reachability to host 'localhost' can be determined
2011-11-09 08:00:50.973 myapp[32572:207] D app:myappAppDelegate.m:190 Begining object mapping...
2011-11-09 08:00:50.974 myapp[32572:207] *** Assertion failure in -[RKManagedObjectMapping initWithEntity:], /Users/alex/Development/myapp/Libraries/RestKit/Code/CoreData/RKManagedObjectMapping.m:32
sharedlibrary apply-load-rules all

这是代码:

// RestKit
#import <RestKit/RestKit.h> 
#import <RestKit/CoreData/CoreData.h>
#import <RestKit/Support/JSON/JSONKit/RKJSONParserJSONKit.h>
#import "DBManagedObjectCache.h"
#import "DBUser.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions    
...
RKLogDebug(@"Initializing Restkit...");


RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:8888"];  
//objectManager.router = [[RKDynamicRouter new] autorelease];  
//manager.router = router;  

objectManager.objectStore = [[[RKManagedObjectStore alloc] initWithStoreFilename:@"mydb.sqlite"] autorelease];
objectManager.objectStore.managedObjectCache = [[DBManagedObjectCache new] autorelease];

RKLogDebug(@"Begining object mapping...");
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[DBUser class]];
RKLogDebug(@"Begining object mapping... 1");
userMapping.primaryKeyAttribute = @"userID";
userMapping.setDefaultValueForMissingAttributes = YES; // clear out any missing attributes (token on logout)

...

这是在 RKManagedObjectMapping.m 中失败的地方:

- (id)initWithEntity:(NSEntityDescription*)entity 
NSAssert(entity, @"Cannot initialize an RKManagedObjectMapping without an entity. Maybe you want RKObjectMapping instead?");
... 

有什么想法吗?谢谢 !!!! 呸呸呸

【问题讨论】:

【参考方案1】:

感谢 tegbains 对 restkit 的介绍,真正解决问题的是在 DataModel.xcdatamodel 中创建实体 DBUser :)

【讨论】:

【参考方案2】:

我认为你没有使用 CoreData。您应该考虑使用RKObjectMapping 和相关的非托管对象和对象映射,按照Tutorial

【讨论】:

以上是关于Restkit“*** -[RKManagedObjectMapping initWithEntity:] 中的断言失败”的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 0.20:restkit 对象映射使属性映射加倍

将“import <RestKit/RestKit.h>”添加到 AppDelegate.m 的问题

RestKit 与直接 CoreData

Restkit 0.20.x cocoapods 安装问题 - 编译但找不到 RestKit 的导入

RestKit / Core Data / Offline - 我需要 UUID 还是 RestKit 足够聪明?

RestKit 核心数据集成