在没有互联网连接的情况下加载数据时出错(Restkit)

发布于 2024-12-28 15:28:26 字数 6036 浏览 0 评论 0原文

通过 RKObjectManager 进行其余调用后,当我没有互联网连接时,它不会加载对象。我用domain.local 进行测试,但我的WIFI 已关闭。

我知道我可以用“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。

After making the rest call through RKObjectManager,it is not loading the objects when i don't have internet connection. I test with domain.local and my WIFI is Off.

I know that i can implement with "reachabilityObserver" but i don't know how can i make this.

My code :

#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];
}

Error :

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}

Thank you for your help.
AO.

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

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

发布评论

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

评论(3

游魂 2025-01-04 15:28:26

这是在 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!");
    }
}

This is how you register for the reachability notifications in RestKit:

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

And here you catch the notification:

- (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!");
    }
}
天气好吗我好吗 2025-01-04 15:28:26

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

因此,如果没有检测到互联网连接,您的请求将失败,并且该委托(您可能已经实现)将被调用:

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

请注意,该委托也可能因其他原因而被调用,例如当您的映射中存在错误时数据。但对于简单的情况,对于你想做的事情来说应该没问题!

You don't even have to register to Restkit reachability notifications system because it is already implemented in the RKRequest method.

So, if no internet connection is detected, your request will fail and this delegate (that you may have already implemented) is called :

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

Beware, this delegate can also be called for others reasons, like when there is an error in the mapping of your data. But for simple cases it should be ok for what you want to do !

木森分化 2025-01-04 15:28:26

显然,如果没有 Iternet 连接,它不会从 Internet 加载数据。您可能想尝试预先实现的 Reachability 类之一,例如: http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html#//apple_ref/doc/uid/DTS40007324-Classes_Reachability_h-DontLinkElementID_5

Obviously, it won't load data from the Internet if there's no Iternet connection. You may want to try one of the pre-implemented Reachability classes, for example: http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html#//apple_ref/doc/uid/DTS40007324-Classes_Reachability_h-DontLinkElementID_5

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