RestKit 0.9.3 app Structure

2019-04-15 06:48发布

问题:

thanks in advance and apologies because newbi questions.

I'm new to restkit and ios development.

My question where to put the mapping of entities.

In the appdelegate.. I know how to do this but then how can i access the object mapping from any viewcontroler.

I have also tried to do the mapping in the object but the RKobject was deprecated, so now I have to extend from NSObject { but I really dont know where to put the mapping with this approach.

Also where is the best place to put the RKObjectManager, do I have to create a new instance in every view controler?

Hope you can help me specially with the mapping things.

回答1:

for using RKObjectManager you create an instance only when you call objectManagerWithBaseURL method. In your appDelegate.

RKObjectManager * restKitManager = [RKObjectManager objectManagerWithBaseURL:@"http://toto/v1/ui"];

After you can use [RKObjectManager sharedManager] to access manager, just import RestKit in you object where your want use it :

[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;

Personally I make an Object just to manage mapping, and I can give you an exemple for my login mapping methode :

-(void)mappingLogin
{
log_debug("mappingLogin")
RKObjectMapping * userMapping = [RKObjectMapping mappingForClass:[VOUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[userMapping mapKeyPath:@"ref" toAttribute:@"ref"];
[userMapping mapKeyPath:@"login" toAttribute:@"login"];
[userMapping mapKeyPath:@"mail" toAttribute:@"mail"];


RKObjectMapping * gatewayMapping = [RKObjectMapping mappingForClass:[VOGateway class]];
[gatewayMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[gatewayMapping mapKeyPath:@"serial" toAttribute:@"serial"];
[gatewayMapping mapKeyPath:@"status" toAttribute:@"status"];

RKObjectMapping * authReturnMapping = [RKObjectMapping mappingForClass:[VOAuth class]];
[authReturnMapping mapKeyPath:@"sessionId" toAttribute:@"sessionId"];
[authReturnMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping];
[authReturnMapping mapKeyPath:@"gateway" toRelationship:@"gateway" withMapping:gatewayMapping];

[[RKObjectManager sharedManager].mappingProvider setMapping:authReturnMapping forKeyPath:@""];

}

If you see the last line you can see that I use [RKObjectManager sharedManager] to set my mapping, I don't create other instance.

It depends of the structure of the code but I do not use directly restik in my viewsController but I have a layer that manages RestKit, in my view I call the methods corresponding to resouces.

If you want to clarify some points, tell me. (If you need some help for specific exemple details objets you want to map ).

Edit for other questions :

1) Exemple of use of returned objects :

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {

    log_debug("##### BackEnd - %@ - %i #####",objectLoader.resourcePath,objectLoader.response.statusCode)

    if ([objectLoader.response isSuccessful]) {

        if ([objectLoader wasSentToResourcePath:@"/auth"]) {
            VOAuth * auth = [objects objectAtIndex:0];
            [BESessionManager getInstance].auth = auth;
            [[NSNotificationCenter defaultCenter] postNotificationName:kSuccessLoginPostLogin object: nil];
        } else if ([objectLoader wasSentToResourcePath:@"/list1/0"]) {
            log_debug("count %i",[objects count])
        }

    } 

}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {

    log_debug("!!!!! BackEnd - %@ - %i !!!!!",objectLoader.resourcePath,objectLoader.response.statusCode)

    if ([objectLoader wasSentToResourcePath:@"/auth"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kErrorLoginPostLogin object: nil];
    }

}

2 ) For use [RKObjectManager sharedManager], import API in your Object .h

#import <RestKit/RestKit.h>

Edit @Neruja Joseph :

  • BESessionManager is my data manager, where I keep all the data loaded by Restkit. This object is a singleton. So the single instance of this object can be reachable in all my views if I import this one :

    import "BESessionManager.h"

  • So, in my callback function I save data in my BESessionManager and when it's done, I send notification from my callback function :

    [[NSNotificationCenter defaultCenter] postNotificationName:kSuccessLoginPostLogin object: nil];

  • In my view who need to display data, or use data I take this in order :

1 - If I use restkit for the first time, when I start app, I init my restKit manager with 'mapping' 'serialization' and global options as baseUrl, certificatValidation, serializationMIMEType, log configuration, ... there is my BERestKitConfig who is singleton too.

2 - If I have services for login I have another singleton named BEServiceUser, in this one I add a method for each related services for login. We can have -(void)postLogIn -(void)getLogout -(void)getUserInfo ...

#import <Foundation/Foundation.h>

// Mandatory class for services
#import <RestKit/RestKit.h>
#import "BESessionManager.h"
#import "BERestKitConfig.h"

// Value Objects
#import "VOUser.h"
#import "VOGateway.h"
#import "VOAuth.h"

// Send Objects
#import "SOAuth.h"


@interface BEServiceUser : NSObject <RKObjectLoaderDelegate> {
    SOAuth * logObj;
}

@property (nonatomic, retain) SOAuth * logObj;

//Singleton
+(BEServiceUser *)getInstance;
+(void)resetInstance;

// CallBack
-(void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
-(void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;

// Services
-(void)postLogin:(NSString*)login  withPassword:(NSString*)password;
-(void)getLogout;

@end

for exemple in my viewDidLoad :

[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(onLoginSuccess) name: kSuccessLoginPostLogin object: nil];
[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(onLoginError) name: kErrorLoginPostLogin object: nil];
[[BEServiceUser getInstance] postLogin:@"toto" withPassword:@"toto"];

3 - if good result, onLoginSuccess method is called in my view. So i can take data in my view from my session manager like this :

[BESessionManager getInstance].auth;