gpt4 book ai didi

ios - 获取json数据

转载 作者:行者123 更新时间:2023-11-29 03:34:45 24 4
gpt4 key购买 nike

我正在开发一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com