在我的SendData类中,有一个以下retrieveData方法。我从MovieViewController调用此类。该方法检查从服务器获取的数据是否正确,如果正确,则返回TRUE。如果不是,则返回FALSE。它工作得很好,只不过该方法在数据有时间“下载”之前返回到我的MovieViewController。因此,该方法始终返回false,因为它始终为null,因为控制权已返回到原始视图控制器。
仅当数据已完成加载而不是之前完成时,如何返回(又返回具有返回值的方法)?
谢谢!
SendData.m
-(BOOL)retrieveData:(NSString *) postID:(NSString *) message:(NSString *)token
{
__block BOOL value=FALSE;
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{
@"message" : message,
@"access_token": token
};
postID=[NSString stringWithFormat:@"https://graph.facebook.com/%@/comments?message=%@&access_token=%@",postID,message,token];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postID]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//THIS NEEDS TO BE EXECUTED BEFORE THE METHOD IS RETURNED
BOOL valid;
NSString* myString;
myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"MY string is %@", myString);
if (myString != NULL || [myString length] != 0)
{
valid = TRUE;
}
else
{
valid = FALSE;
}
value=valid;
}];
return value;
}
您可以使用委托模式来做到这一点。您必须将以下属性添加到SendData
@property (nonatomic, weak) id delegate;
和方法:
- (void)dataDownloaded:(NSNumber *)result
到MovieViewController
。
然后,MovieViewController
您必须进行设置(我在这里假设sendData是SendData类的对象):
sendData.delegate = self;
然后,您必须将定义更改为:
-(void)retrieveData:(NSString *) postID:(NSString *) message:(NSString *)token
{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{
@"message" : message,
@"access_token": token
};
postID=[NSString stringWithFormat:@"https://graph.facebook.com/%@/comments?message=%@&access_token=%@",postID,message,token];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postID]];
request.HTTPMethod = @"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//THIS NEEDS TO BE EXECUTED BEFORE THE METHOD IS RETURNED
BOOL valid;
NSString* myString;
myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"MY string is %@", myString);
if (myString != NULL || [myString length] != 0)
{
valid = TRUE;
}
else
{
valid = FALSE;
}
[self.delegate performSelectorOnMainThread:@selector(dataDownloaded:) withObject:@(valid) waitUntilDone:NO];
}];
}
您还可以检查是否self.delegate
响应dataDownloaded:
:
[self.delegate respondsToSelector:@selector(dataDownloaded:)]
另外,也可以使用块或代替委托模式NSNotificationCenter
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句