首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脸书SLRequest搅乱NSURLConnection

脸书SLRequest搅乱NSURLConnection
EN

Stack Overflow用户
提问于 2013-10-28 11:15:24
回答 2查看 523关注 0票数 3

我正在尝试使用SLRequest iOS应用程序接口获取facebook数据,它似乎工作得很好-

代码语言:javascript
复制
NSDictionary *parameters = @{};
NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/home"];

SLRequest *feedRequest = [SLRequest 
    requestForServiceType:SLServiceTypeFacebook
    requestMethod:SLRequestMethodGET
    URL:feedURL 
    parameters:parameters];

feedRequest.account = facebookAccount;

[feedRequest performRequestWithHandler:^(NSData *responseData, 
       NSHTTPURLResponse *urlResponse, NSError *error)
{
    // something
}];

然而,在此之后,我向我的一台服务器发出了一个HTTP POST请求,

代码语言:javascript
复制
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSData *requestData = [NSData dataWithBytes:[jsonData bytes] length:[jsonData length]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

它可以很好地发布数据(从服务器日志中验证),但我在

代码语言:javascript
复制
connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response

这曾经在没有SLRequest帖子的情况下工作得很好,我可以对它进行注释,然后它又开始工作了。

这里我漏掉了什么?

EN

回答 2

Stack Overflow用户

发布于 2013-12-03 07:46:30

如果你创建一个强引用到你的NSURLConnection,我猜iOS正在释放这个对象,所以这就是为什么你的委托永远不会被调用。

因此,不是:

代码语言:javascript
复制
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

使用:

代码语言:javascript
复制
self.connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

其中:

代码语言:javascript
复制
@property(nonatomic, strong)NSURLConnection * connection;

告诉我进展如何,干杯。

亲切的问候

票数 2
EN

Stack Overflow用户

发布于 2013-11-26 20:37:26

对我来说,修复方法是不使用SLRequest块加载,而是在第一时间使用带有适当NSURLConnectionDelegate方法的NSURLConnection。第二次使用这个委托时,加载应该没问题,尽量让所有的加载都放在同一个实用类中,这样它们就可以共享同一个委托了。对我来说,这是twitters v1.1API的问题。

因此,在您的代码中,尝试删除块,然后需要准备SLRequest:

代码语言:javascript
复制
urlData=[[NSData alloc] init];
// change that SLRequest to a NSURLRequest
NSURLRequest *request = [feedRequest preparedURLRequest];
dispatch_async(dispatch_get_main_queue(), ^{
    [NSURLConnection connectionWithRequest:request delegate:self];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
});

然后使用NSURLConnectionDelegate方法,捕获加载的NSData:

代码语言:javascript
复制
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    connection=nil;
    NSError *jsonParsingError = nil;
    NSMutableDictionary *deserializedData  = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingAllowFragments error:&jsonParsingError];
    if (deserializedData) {
         // handle the loaded dictionary as you please
    } 
}

这个修复的灵感来自于基思·哈里森的帖子:http://useyourloaf.com/blog/2013/06/24/migrating-to-the-new-twitter-search-api.html

所以功劳也归功于他。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19626665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档