I am using AFNetworking to fetch data and I have a delegate in the Service class which could be a UIController. The Service class is responsible for fetching data through AFNetworking classes.
//Class Service.
@interface Service : NSObject {
__weak id<ServiceDelegate>delegate;
}
- (void) doFetch:(APIInput*) input {
NSURL *url = [NSURL URLWithString:[self getLocation:input]];
NSLog(@"url request is: %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
[self.delegate serviceDidComplete:[self allocNewData:JSON]];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON){
[self.delegate serviceFailed: error];
}];
[operation start];
}
And in the UIController viewDidLoad: I have the following
self.networkService = [[Service alloc] init];
[self.networkService setDelegate:self];
Once I run the ios app, self.delegate is nil. Why is this happening?
This solved it. I created a __block variable before setting the operation
__block id itsme = self.delegate;
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
[itsme serviceDidComplete:[self allocNewData:JSON]];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON){
[itsme serviceFailed: error];
}];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.