我是Core Data和RestKit的新手,我已经阅读过很多关于Core Data和RestKit的教程,但我仍然无法从服务器获取数据。在我的应用程序中,我需要获取数据,将其存储在设计中并从数据库加载如果有或没有互联网连接,我知道RestKit可以为我做,但当我尝试从{{3我看到一些错误,没有任何反应,请帮助我,我做错了什么?我将非常感谢任何有用的参考资料。谢谢。
我有Model.xxdatamodelid和" SCategory"实体
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface SCategory : NSManagedObject
@property (nonatomic, retain) NSString * cName;
@property (nonatomic, retain) NSNumber * cId;
@property (nonatomic, retain) NSString * cSection;
@property (nonatomic, retain) NSString * cImage;
@end
AppDelegate.m中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
NSError *error = nil;
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[managedObjectStore createManagedObjectContexts];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://salatiki.com.ua"]];
manager.managedObjectStore = managedObjectStore;
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SCategory class]) inManagedObjectStore:managedObjectStore];
[categoryMapping addAttributeMappingsFromDictionary:@{ @"img": @"cImage",
@"section": @"cSection",
@"vid" : @"cId",
@"vname" : @"cName" }];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping method:RKRequestMethodGET pathPattern:@"/api/get.php?getSaladsType" keyPath:nil statusCodes:statusCodes];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
[manager getObjectsAtPath:@"/api/get.php?getSaladsType" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"map arr: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"request error");
}];
// Override point for customization after application launch.
return YES;
}
日志:
014-11-02 13:31:41.742 Salatiki[2000:3713] E restkit.network:RKObjectRequestOperation.m:213 GET 'http://salatiki.com.ua/api/get.php?getSaladsType'
(200 OK / 0 objects) [request=0.0000s mapping=0.0000s total=0.0267s]: Error
Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the
response loaded." UserInfo=0x7f9fb883c880 {NSLocalizedFailureReason=A 200 response was
loaded from the URL 'http://salatiki.com.ua/api/get.php?getSaladsType', which failed to
match all (0) response descriptors:, NSErrorFailingURLStringKey=http://salatiki.com.ua/api/get.php?getSaladsType,
NSErrorFailingURLKey=http://salatiki.com.ua/api/get.php?getSaladsType,
NSUnderlyingError=0x7f9fb883c3c0 "No mappable object representations were found at the
key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match
the response loaded.}
2014-11-02 13:31:41.766 Salatiki[2000:613] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://salatiki.com.ua/api/get.php?
getSaladsType'
2014-11-02 13:31:41.766 Salatiki[2000:613] request error
答案 0 :(得分:0)
您正在努力使用http://salatiki.com.ua/api/get.php?getSaladsType
的网址,因为您在查询参数中添加了很多含义,并且RestKit不会将查询参数用于响应描述符模式。
如果你只对这一个请求使用get.php
,那么你可以从响应描述符路径模式中删除?getSaladsType
,它应该可以工作。如果你将它用于多件事情,那么你需要停止使用RestKit,更改服务器,这样你就不会使用这样的查询参数或者编辑RestKit ...
答案 1 :(得分:0)
最后我找到了解决方案,所有问题都在RKResponseDescriptor
,pathPattern
在我的情况下应该为零,我不知道为什么,但它有效!