RestKit 0.9.3 应用程序结构

发布于 2024-12-22 07:03:23 字数 325 浏览 0 评论 0原文

预先感谢并道歉,因为新手提出问题。

我是restkit和ios开发的新手。

我的问题是将实体的映射放在哪里。

在appdelegate中..我知道如何做到这一点,但是我如何从任何视图控制器访问对象映射。

我也尝试过在对象中进行映射,但 RKobject 已被弃用,所以现在我必须从 NSObject { 扩展,但我真的不知道用这种方法将映射放在哪里。

另外,放置 RKObjectManager 的最佳位置在哪里,我是否必须在每个视图控制器中创建一个新实例?

希望你能特别帮助我处理地图方面的事情。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2024-12-29 07:03:23

对于使用 RKObjectManager,仅当调用 objectManagerWithBaseURL 方法时才创建实例。在你的应用程序中Delegate.

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

在您可以使用 [RKObjectManager sharedManager] 访问管理器之后,只需在您想要使用它的对象中导入 RestKit :

[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;

我个人创建一个对象只是为了管理映射,我可以给您一个我的登录映射方法的示例:

-(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:@""];

}

如果您看到最后一行您可以看到我使用 [RKObjectManager sharedManager] 来设置我的映射,我不创建其他实例。

这取决于代码的结构,但我不直接在我的viewsController中使用restik,但我有一个管理RestKit的层,在我看来,我调用与资源相对应的方法。

如果您想澄清一些问题,请告诉我。 (如果您需要一些有关要映射的特定示例详细信息对象的帮助)。

编辑其他问题:

1)返回对象的使用示例:

- (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)要使用[RKObjectManager共享管理器],请在您的对象.h中导入API

#import <RestKit/RestKit.h>

编辑@Neruja Joseph:

  • BESessionManager是我的数据管理器,我保存 Restkit 加载的所有数据。该对象是一个单例对象。因此,如果我导入此对象,则可以在我的所有视图中访问该对象的单个实例:

    import "BESessionManager.h"

  • 因此,在我的回调函数中,我将数据保存在 BESessionManager 中,完成后,我从我的回调函数:

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

  • 在我看来,谁需要显示数据,或使用数据,我按顺序:

1 - 如果我使用restkit第一次,当我启动应用程序时,我使用“映射”“序列化”和全局选项作为baseUrl、certificatValidation、serializationMIMEType、日志配置等初始化restKit管理器,...这是我的BERestKitConfig也是单例。

2 - 如果我有登录服务,我有另一个名为 BEServiceUser 的单例,在这个单例中,我为每个相关的登录服务添加一种方法。我们可以有 -(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

例如在我的 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 - 如果结果良好,在我看来会调用 onLoginSuccess 方法。所以我可以从我的会话管理器中获取我的视图中的数据,如下所示:

[BESessionManager getInstance].auth;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文