简体   繁体   English

试图在UITableView中加载大量的JSON数据需要花费很多时间

[英]Trying to load large amount of JSON data in UITableView taking much time

I'm new to iOS and have been trying to load JSON data in UITableView (within a UIViewController, which are in a Tab bar controller). 我是iOS的新手,并且一直在尝试在UITableView中加载JSON数据(在UIViewController中,它们位于Tab栏控制器中)。 The JSON data is huge and I have tried using AFNetworking library as well. JSON数据很大,我也尝试过使用AFNetworking库。 But still the data takes more that 15 secs. 但数据仍需要15秒以上。 to load. 装载。

Here is what I've tried: Approach #2 这是我尝试过的方法:方法#2

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.


[self saveData];



return YES;
}
-(void) saveData
{
NSString * userType= @"3" ; 
NSString * userID= @"33" ;
NSString * URLString = [NSString stringWithFormat:@"http://<some_url>/%@/%@/",userType,userID];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer =[AFHTTPResponseSerializer serializer];
[manager GET:leadsURLString parameters:nil progress:nil success:^(NSURLSessionTask *task, NSData* responseObject) {
    NSError * error = nil;
    NSArray * lData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
    NSLog(@"Leads:%@",lData);


    if (error != nil) {
        NSLog(@"JSON Error: %@", [error localizedDescription]);
    }
    // Write the file .
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * docDirectory = [paths objectAtIndex:0];
    NSString * lFilePath = [NSString stringWithFormat:@"%@",[docDirectory stringByAppendingPathComponent:@"leads"]]; // File

    if (![leadsData writeToFile:lFilePath atomically:YES]) {
        NSLog(@"Couldn't save leads data.");
    }

} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

}
@end
Code for tableView
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docDirectory = [paths objectAtIndex:0];
NSString * projFilePath = [NSString stringWithFormat:@"%@",[docDirectory stringByAppendingPathComponent:@"leads"]]; // file
if ([[NSFileManager defaultManager] fileExistsAtPath:projFilePath])
{
    _projects = [[NSMutableArray alloc] initWithContentsOfFile:projFilePath];

}
else
{
    _projects = [NSMutableArray arrayWithObject:[NSDictionary dictionaryWithObject:@"No Project" forKey:@"project_name"]];
}
[self.tableView reloadData];

}
  1. Initially tried loading data when the view controllers load, took too long. 最初尝试在视图控制器加载时加载数据,耗时太长。

  2. Then tried loading data in Appdelegate and saving it, and loading it when the view controllers load, still took long enough. 然后尝试在Appdelegate中加载数据并保存它,并在视图控制器加载时加载它仍然需要足够长的时间。

  3. Tried loading data in Appdelegate and then setting it in the respective view controllers, but couldn't achieve it. 尝试在Appdelegate中加载数据,然后在相应的视图控制器中设置它,但无法实现。

Could anyone suggest the best way to accomplish it? 有谁能建议最好的方法来完成它?

Try to follow below steps to reduce the time. 尝试按照以下步骤减少时间。

Step: 1 Try to load data from below check how time it will take. 步骤:1尝试从下面加载数据,检查时间。

                 AFHTTPSessionManager *managerFB = [AFHTTPSessionManager manager];
                 [managerFB GET:WEB_SERIVCE_URL parameters:dicData progress:^(NSProgress * _Nonnull downloadProgress) {
                     <#code#>
                 } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                     NSLog(@"%@",responseObject);
                 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                     <#code#>
                 }];

Step: 2: Don't try to save data into local file. 步骤2:不要尝试将数据保存到本地文件中。 Create one global variable in AppDelegate and stored directly into it. 在AppDelegate中创建一个全局变量并直接存储到其中。

Step: 3: Try to call service in background thread, so your main application UI won't be stuck. 步骤3:尝试在后台线程中调用服务,这样您的主应用程序UI就不会卡住。

[self performSelector:@selector(yourMethodName) withObject:nil afterDelay:0.2];

Also, you can check your web-service response time in Postman which is extension of Chrome. 此外,您还可以在Postman中检查您的网络服务响应时间,这是Chrome的扩展程序。 So you can get response time from service. 所以你可以从服务中获得响应时间。

if data is much larger(as you mentioned in comment) then you can manage your api something like you can send last index to server and server returns data from that index and you can define total number of records should be return in one call. 如果数据要大得多(正如您在评论中提到的那样),那么您可以管理您的api ,就像您可以将最后一个索引发送到服务器并且服务器从该索引返回数据,您可以定义应在一次调用中返回的记录总数。 then you can frequently call api as user scrolls your table view! 然后,当用户滚动您的表格视图时,您可以经常调用api

For example you could consider facebook app's time line or Quora app's read tab! 例如,您可以考虑facebook app's time lineQuora app's read选项卡!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM