2012-01-24 3 views
1

RKObjectManager를 통해 나머지를 호출 한 후 인터넷에 연결되어 있지 않을 때 객체를로드하지 않습니다. domain.local로 테스트하고 WIFI는 Off입니다.인터넷 연결없이 데이터를로드하는 중 오류가 발생했습니다 (Restkit)

"reachabilityObserver"로 구현할 수 있음을 알고 있지만 어떻게해야 할 지 모르겠습니다.

내 코드 :

#import "ViewInformationForm.h" 
#import <RestKit/RestKit.h> 

@interface User : NSObject { 
    NSNumber*  _user_forfait; 
    NSNumber*  _user_client_free; 
    NSNumber*  _user_demande_portabilite; 
    NSNumber*  _user_mail_confirm; 
    NSNumber*  _user_mail_enregistrement_inscrption; 
    NSNumber*  _user_mail_depart_expedition; 
    NSNumber*  _user_mail_arrivee_expedition; 
    NSNumber*  _user_activation; 
    NSNumber*  _user_portabilite; 
} 

@property (nonatomic, retain) NSNumber*  user_forfait; 
@property (nonatomic, retain) NSNumber*  user_client_free; 
@property (nonatomic, retain) NSNumber*  user_demande_portabilite; 
@property (nonatomic, retain) NSNumber*  user_mail_confirm; 
@property (nonatomic, retain) NSNumber*  user_mail_enregistrement_inscrption; 
@property (nonatomic, retain) NSNumber*  user_mail_depart_expedition; 
@property (nonatomic, retain) NSNumber*  user_mail_arrivee_expedition; 
@property (nonatomic, retain) NSNumber*  user_activation; 
@property (nonatomic, retain) NSNumber*  user_portabilite; 

@end 

@implementation User 

@synthesize user_forfait = _user_forfait; 
@synthesize user_client_free = _user_client_free; 
@synthesize user_demande_portabilite = _user_demande_portabilite; 
@synthesize user_mail_confirm = _user_mail_confirm; 
@synthesize user_mail_enregistrement_inscrption = _user_mail_enregistrement_inscrption; 
@synthesize user_mail_depart_expedition = _user_mail_depart_expedition; 
@synthesize user_mail_arrivee_expedition = _user_mail_arrivee_expedition; 
@synthesize user_activation = _user_activation; 
@synthesize user_portabilite = _user_portabilite; 

@end 


@implementation ViewInformationForm; 
@synthesize picker,pickerDate, forfaitNames; 
@synthesize forfaitText, TextDateEnregistrement; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
- (void)LoadData { 


    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class]]; 
    [mapping mapKeyPathsToAttributes: 
    @"data.user_forfait", @"user_forfait", 
    @"data.user_client_free", @"user_client_free", 
    @"data.user_demande_portabilite", @"user_demande_portabilite", 
    @"data.user_mail_confirm", @"user_mail_confirm", 
    @"data.user_mail_enregistrement_inscrption", @"user_mail_enregistrement_inscrption", 
    @"data.user_mail_depart_expedition", @"user_mail_depart_expedition", 
    @"data.user_mail_arrivee_expedition", @"user_mail_arrivee_expedition", 
    @"data.user_activation", @"user_activation", 
    @"data.user_portabilite", @"user_portabilite", 
    nil]; 

    RKObjectManager *objectManager = [RKObjectManager sharedManager]; 


    NSString* urlUID = [NSString stringWithFormat:@"/user/data?uid=%@",uuid]; 
    RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:urlUID delegate:self]; 
    objectLoader.method = RKRequestMethodGET; 
    objectLoader.objectMapping = mapping; 
    [objectLoader send]; 

} 


- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { 
    User* user = [objects objectAtIndex:0]; 

    NSString* info = [NSString stringWithFormat: 
         @"\n user_forfait : %@ \n" 
         @"user_client_free : %@",[user user_forfait], [user user_client_free]]; 
    NSLog(@"%@",info); 
} 

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error { 
    RKLogError(@"Load of RKRequest %@ failed with error: %@", objectLoader, error);      
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView 
{ 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [RKObjectManager objectManagerWithBaseURL:gRKCatalogBaseURL]; 



    [self LoadData]; 
    [super viewDidLoad]; 
    self.forfaitNames = [[NSArray alloc] initWithObjects: 
         @"-- Sélectionnez un forfait --", @"Forfait 19,99 €", @"Forfait 15,90 €", @"Forfait 2 €", 
         @"Forfait 0 €", nil]; 
} 

오류 :

2012-01-24 09:59:32.120 Free M. Stats[13438:10703] I restkit:RKLog.m:30 RestKit initialized... 
2012-01-24 09:59:32.127 Free M. Stats[13438:10703] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x8987300 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=652464 reachabilityFlags=-R tc-----> 
2012-01-24 09:59:32.129 Free M. Stats[13438:10703] E restkit.network:RKRequest.m:464 Failed to send request to http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c due to unreachable network. Reachability observer = <RKReachabilityObserver: 0x8987300 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=652464 reachabilityFlags=-R tc-----> 
2012-01-24 09:59:32.130 Free M. Stats[13438:10703] E app:ViewInformationForm.m:102 Load of RKRequest <RKObjectLoader: 0x6e69050> failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=2 "The client is unable to contact the resource at http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c" UserInfo=0x6c99270 {NSLocalizedDescription=The client is unable to contact the resource at http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c} 

당신의 도움에 감사드립니다. AO.

답변

2

이것은 당신이 RestKit에 도달 알림을 등록하는 방법입니다 : 여기

// Register for changes in network availability 
NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; 
[center addObserver:self selector:@selector(reachabilityDidChange:) name:RKReachabilityDidChangeNotification object:nil]; 

그리고 당신이 알림을 잡을 :

- (void)reachabilityDidChange:(NSNotification *)notification { 
    RKReachabilityObserver* observer = (RKReachabilityObserver *) [notification object]; 
    RKReachabilityNetworkStatus status = [observer networkStatus]; 
    if (RKReachabilityNotReachable == status) { 
     RKLogInfo(@"No network access!"); 
    } else if (RKReachabilityReachableViaWiFi == status) { 
     RKLogInfo(@"Online via WiFi!"); 
    } else if (RKReachabilityReachableViaWWAN == status) { 
     RKLogInfo(@"Online via Edge or 3G!"); 
    } 
} 
+0

감사합니다. Observer를 삭제하는 방법을 알고 있습니까? 왜냐하면 다음 번에 내가보기로 돌아 왔을 때 Didload View에서 alertview를 실행하면 2가 2가되기 때문입니다. –

+0

이렇게해야합니다 :'NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; [center removeObserver : 자체 이름 : RKReachabilityDidChangeNotification 객체 : nil]; ' – lawicko

+0

viewDidLoad에서 addObserver를 배치하고 viewDidUnload에서 removeObserver를 배치 할 때와 동일한 문제가 있습니다. 옵저버는 비어 있지 않으며 내 alertview는 X 번 더 표시됩니다. –

1

RKRequest 메서드에 이미 구현되어 있으므로 Restkit 도달 가능성 알림 시스템에 등록 할 필요조차 없습니다.

그래서 인터넷이 연결되지 않은 경우, 요청이 실패하고이 대리인이 (이미 구현 한 수 있음)라고 :

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error { 
    // No internet ! 
} 

은 조심,이 대표는 또한 다른 이유로 호출 할 수 있습니다, 데이터 매핑에 오류가있는 경우와 같습니다. 그러나 간단한 경우에 당신이하고 싶은 일은 괜찮을 것입니다!

관련 문제