简体   繁体   中英

RestKit 0.20 can't get data from server

I'm new in Core Data and in RestKit, I had already read many tutorials about Core Data and about RestKit, but i still can't get my data from server. In my app i need to get data, store it on devise and load in from DB if there is or there is no internet connection, i know that RestKit can do that for me, but when i try to get my data from http://salatiki.com.ua/api/get.php?getSaladsType i see some errors and nothing happens, please help me, what i'm doing wrong? I will be very grateful for any useful references. Thank you.

i have Model.xxdatamodelid with "SCategory" entity

#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

in 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;
}

log:

    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

You're going to struggle with a URL of http://salatiki.com.ua/api/get.php?getSaladsType because you are putting a lot of meaning into the query parameters and RestKit doesn't use query parameters for response descriptor patterns.

If you're only using get.php for this one request then you can just remove ?getSaladsType from the response descriptor path pattern and it should work. If you're using it for multiple things then you'll need to stop using RestKit, change the server so you don't use query parameters like that or edit RestKit...

最后,我找到了解决方案,所有问题都在RKResponseDescriptor ,在我的情况下pathPattern应该为nil,我不知道为什么,但是它有效!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM