iphone - IOS5 - AFNetworking 处理请求

标签 iphone objective-c ios request afnetworking

我正在制作一个应用程序,我必须在其中调用一些网络服务。我选择与 AFNetworking 一起工作.

我遵循了库中提供的 Twitter 示例。一切正常,除了我在通知栏中永久有一个小“处理圈”(见下图)。

iPhone topbar

这是我的请求代码:

- (id)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }

    _name = [attributes valueForKeyPath:@"name"];
    return self;
}

+ (void)itemsListWithBlock:(void (^)(NSArray *items))block
{
    NSUserDefaults *defaults        = [NSUserDefaults standardUserDefaults];
    NSDictionary *user              = [defaults objectForKey:@"user"];
    NSDictionary *company           = [defaults objectForKey:@"company"];

    NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];

    /*
    ** [ Some stuff to set the parameters in a NSDictionnary ]
    */    

    MyAPIClient *client = [MyAPIClient sharedClient];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSURLRequest *request = [client requestWithMethod:@"POST" path:@"getMyList" parameters:mutableParameters];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSMutableArray *mutableItems = [NSMutableArray arrayWithCapacity:[JSON count]];
        for (NSDictionary *attributes in JSON) {
            ListItem *item = [[ListItem alloc] initWithAttributes:attributes];
            [mutableItems addObject:item];
        }
        if (block) {
            block([NSArray arrayWithArray:mutableItems]);
        }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
        if (block) {
            block(nil);
        }
    }];
    [operation start];
}

这是否意味着我的请求未完成?我并没有真正理解我在这里做错了什么......

如果有人能提供帮助,我将不胜感激。谢谢。

最佳答案

不要调用 [[AFNetworkActivityIndi​​catorManager sharedManager] incrementActivityCount]; 这会将事件计数增加 1,[operation start]; 也会调用它。现在事件计数为 2,并且在操作完成后会减少。但由于您调用了 incrementActivityCount,它会将其恢复为 1 而不是 0。

只需调用 [[AFNetworkActivityIndi​​catorManager sharedManager] setEnabled:YES]; 一次,例如将它放在应用程序 appDeletage 的 application:applicationdidFinishLaunchingWithOptions: 方法中。


此外,我建议将操作添加到 NSOperationQueue 而不仅仅是在其上调用 start。

关于iphone - IOS5 - AFNetworking 处理请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985001/

相关文章:

iphone - 如何在iPhone应用程序的表格单元格中加载背景图像

iphone - 为什么将单元格图像添加到 UITableViewCell 会发出警告?

ios - IOS实现Oauth2.0流程

objective-c - 从 GCC 中的单独文件内联函数

ios - iOS 中以下情况有什么区别?

ios - 使用动画显示图像的部分内容

ios - ARKit - 如何从服务器 URL 加载 .scn 和纹理文件

iphone - NSCFTimer 是否存在内存泄漏?

javascript - UIWebView-stringByEvaluatingJavaScriptFromString : fill textbox in iFrame content?

ios - 为什么我们在react-native中使用ProgressViewIOS组件的progressImage属性?