获取json数据

时间:2013-10-13 05:22:54

标签: ios json nsurlrequest

我正在开发iOS应用

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

一旦我使用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方法需要花时间来获取图片......所以它会冻结我的应用

所以,我应该怎么做以获取个人资料图片而不是冻结我的应用程序?

1 个答案:

答案 0 :(得分:1)

您的代码使用的是NSURLConnection的异步方法。因此,部分代码不会导致代码冻结。如果您的表视图不能平滑滚动,则可能是由于您在主线程中放入的代码(可能是代码在didFinishDownloadLoad中执行的操作)。