我正在制作一个应用程序,我必须在其中调用一些网络服务。我选择与 AFNetworking 一起工作.
我遵循了库中提供的 Twitter 示例。一切正常,除了我在通知栏中永久有一个小“处理圈”(见下图)。
这是我的请求代码:
- (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];
}
这是否意味着我的请求未完成?我并没有真正理解我在这里做错了什么......
如果有人能提供帮助,我将不胜感激。谢谢。
最佳答案
不要调用 [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
这会将事件计数增加 1,[operation start];
也会调用它。现在事件计数为 2,并且在操作完成后会减少。但由于您调用了 incrementActivityCount
,它会将其恢复为 1 而不是 0。
只需调用 [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
一次,例如将它放在应用程序 appDeletage 的 application:applicationdidFinishLaunchingWithOptions:
方法中。
此外,我建议将操作添加到 NSOperationQueue
而不仅仅是在其上调用 start。
关于iphone - IOS5 - AFNetworking 处理请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985001/