2012-01-24 32 views
1

在通过RKObjectManager进行其他调用之后,当我没有连接互联网时,它不会加载对象。我用domain.local测试,我的WIFI关闭。加载数据时没有网络连接时出错(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

谢谢。你知道如何删除观察者,因为如果我在Didload View上启动了一个alertview,那么下次我回到视图上时,会有两个。 –

+0

这应该这样做:'NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; [center removeObserver:self name:RKReachabilityDidChangeNotification object:nil];' – lawicko

+0

当addObserver放在viewDidLoad和removeObserver上的viewDidUnload上时,会出现同样的问题。观察员不是空的,我的alertview显示X更多的时间。 –

1

您甚至不必注册到Restkit可访问性通知系统,因为它已经在RKRequest方法中实现。

所以,如果没有检测到互联网连接,您的请求将会失败,这代表(你可能已经实现)被称为:

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

当心,此委托也可称为对其它方面的原因,就像数据映射出现错误时一样。但对于简单的情况,它应该可以做你想做的事!