IOS5 - AFNetworking处理请求

Nug*_*get 0 iphone objective-c request ios afnetworking

我正在制作一个应用程序,我必须调用一些web服务.我选择与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];
}
Run Code Online (Sandbox Code Playgroud)

这是否意味着我的请求没有完成?我真的不知道我在这里做错了什么......

如果有人可以提供帮助,我真的很感激.谢谢.

rck*_*nes 5

不要 [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];将此活动计数增加为1,并且[operation start];也会调用它.现在活动计数为2,并在操作完成后减少.但是既然你打电话给incrementActivityCount它会把它带回1而不是0.

只需调用[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];一次,例如将其放在application:applicationdidFinishLaunchingWithOptions:应用程序appDeletage 的方法中.


另外我建议将操作添加到a NSOperationQueue而不仅仅是调用start.