สำหรับการใช้ RKObjectManager คุณจะสร้างอินสแตนซ์เฉพาะเมื่อคุณเรียกใช้เมธอด objectManagerWithBaseURL ในแอปของคุณDelegate
RKObjectManager * restKitManager = [RKObjectManager objectManagerWithBaseURL:@"http://toto/v1/ui"];
หลังจากที่คุณสามารถใช้ [RKObjectManager sharedManager] เพื่อเข้าถึงผู้จัดการ เพียงนำเข้า RestKit ในวัตถุที่คุณต้องการใช้:
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
โดยส่วนตัวแล้วฉันสร้าง Object เพื่อจัดการการแมป และฉันสามารถให้ตัวอย่างสำหรับวิธีการแมปการเข้าสู่ระบบของฉันได้:
-(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] เพื่อตั้งค่าการแมป ฉันจะไม่สร้างอินสแตนซ์อื่น
ขึ้นอยู่กับโครงสร้างของโค้ด แต่ฉันไม่ได้ใช้ Restik โดยตรงใน viewerController ของฉัน แต่ฉันมีเลเยอร์ที่จัดการ 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 sharedManager] ให้นำเข้า API ใน Object .h ของคุณ
#import <RestKit/RestKit.h>
แก้ไข @เนรูจา โจเซฟ :
BESessionManager เป็นผู้จัดการข้อมูลของฉัน โดยที่ฉันเก็บข้อมูลทั้งหมดที่โหลดโดย Restkit วัตถุนี้เป็นซิงเกิลตัน ดังนั้นอินสแตนซ์เดียวของวัตถุนี้สามารถเข้าถึงได้ในทุกมุมมองของฉันหากฉันนำเข้าสิ่งนี้:
นำเข้า "BESessionManager.h"
ดังนั้น ในฟังก์ชันการโทรกลับของฉัน ฉันบันทึกข้อมูลใน BESessionManager ของฉัน และเมื่อเสร็จแล้ว ฉันก็ส่งการแจ้งเตือนจากฟังก์ชันการโทรกลับของฉัน:
[[NSNotificationCenter defaultCenter] postNotificationName:วัตถุ kSuccessLoginPostLogin: ไม่มี];
ในมุมมองของฉันว่าใครจำเป็นต้องแสดงข้อมูลหรือใช้ข้อมูล ฉันทำสิ่งนี้ตามลำดับ:
1 - ถ้าฉันใช้ restkit เป็นครั้งแรก เมื่อฉันเริ่มแอป ฉันจะเริ่มต้นตัวจัดการ restKit ด้วย 'การแมป' 'การทำให้เป็นอนุกรม' และตัวเลือกส่วนกลาง เช่น baseUrl, certificatValidation, serializationMIMEType, การกำหนดค่าบันทึก, ... มี 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;
person
Beber
schedule
22.12.2011