ios - 获取json数据

标签 ios json nsurlrequest

我正在开发一个 iOS 应用

我有一个包含一些用户数据的网络服务。

一旦我使用 URLRequest 获取数据,就很顺利,我真正从服务器获取了我想要的数据量。

theRequest = [[NSMutableURLRequest alloc]
                         initWithURL: [NSURL URLWithString:URL]
                         cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
                         timeoutInterval: 10
                         ];

/////////////////

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);

switch (getType) {
    case 0:
    default:
        [delegate performSelectorOnMainThread:@selector(didFinishDownloadLoad:) withObject:receivedData waitUntilDone:false];

但问题是,我需要将数据提取到 uitableviecell。

如果我包含 10 个数据列表,我需要循环 10 次来初始化数据。

它会“卡住”我的应用程序,我在获取数据时什么也做不了

我应该怎么做才能“不卡住”我的应用程序?

////更新**

-(void) didFinishDownloadLoad:(NSMutableData*)receivedData{

NSData *theData = [[NSData alloc] initWithData:receivedData];
NSError *theError = nil;

NSMutableArray *tmparray = [NSMutableArray array];

NSDictionary *theObject = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingAllowFragments error:&theError];
NSLog(@"theObject:%@",theObject);
    
    
NSDictionary *theList = [theObject objectForKey:@"list"];
    
    for (NSString *s in theList)
    {

        NSDictionary *theNewFeed = [theList objectForKey:s];
        NewFeedDetailsManager *newFeedDetails = [[NewFeedDetailsManager alloc] init];
        [newFeedDetails setCid:s];
        [newFeedDetails setFbid:[theNewFeed objectForKey:@"fbid"]];
        [newFeedDetails setFbUserName:[theNewFeed objectForKey:@"fbname"]];
        [newFeedDetails setUserComment:[theNewFeed objectForKey:@"comment"]];
        [newFeedDetails setRestaurantName:[theNewFeed objectForKey:@"name"]];
        [newFeedDetails setRestaurantAddress:[theNewFeed objectForKey:@"address"]];
        [newFeedDetails setWriteDate:[theNewFeed objectForKey:@"date"]];
        [newFeedDetails setSid:[theNewFeed objectForKey:@"sid"]];
        
        //get User Pro Pic
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture", newFeedDetails.fbid]];
        newFeedDetails.userimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        
        [tmparray addObject:newFeedDetails];
    }
}

谢谢大家,现在我明白了问题..

在“获取用户头像”中卡住了 NSURL 方法需要花时间来获取图片...所以它会卡住我的应用程序

那么,我应该怎么做才能获取个人资料图片而不是卡住我的应用程序?

最佳答案

您的代码正在使用 NSURLConnection 的异步方法。所以这部分代码不会导致你的代码卡住。如果您的表格 View 滚动不顺畅,可能是由于您在主线程中放入的代码(可能是 didFinishDownloadLoad 中的代码执行的操作)造成的。

关于ios - 获取json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19342152/

相关文章:

ios - 无法使用 Carthage 或 CocoaPods 链接任何框架

android - 如何使用 Volley 在 JSON 中的数组中获取数组中的元素

iphone - 多个异步 Web 服务请求 NSURLConnection iOS

iphone - NSURLErrorDomain 错误-1012

ios - NSURLConnection 使用 iOS Swift

ios - 除非你改变它,否则你怎么能让照片永远留在那里?

ios - 将动画应用于 UIKit 类的每个实例?

android - Android 2.1中JSONObject的Base64解码

javascript - 从 div 中的 json 文件写入解析后的响应

ios - 支持两种 iPhone 屏幕尺寸的全屏单元