gpt4 book ai didi

iOS NSURL 文件多个请求的排队机制

转载 作者:行者123 更新时间:2023-11-29 03:30:48 25 4
gpt4 key购买 nike

我对 iOS 开发非常陌生,但我想制作一个具有两个 TableView Controller (列)的应用程序:两者都是充当链接的一行图像。第一个是 YouTube 视频列,第二个是网站列。我希望将所有这些都列在文件 file.txt 中,如下所示: V, http://youtube.com/example W, http://example.com

会有一个很长的列表,V 表示它是视频(用于视频栏),W 表示网站。现在,我明白了如何成为单个文件,但之后发生的事情是我关心的。我可以将每一行读入某种队列,然后连续触发每一行的 NSURL 请求吗? NSURL 如何做到这一点?也许有更好的方法吗?

最佳答案

我有两个问题:

  1. 文本文件真的是最好的格式吗?

    我可能会建议使用 plist 或存档(如果该文件仅存在于您的应用程序的 bundle 和/或文档文件夹中)或 JSON(如果它在将其交付给用户之前将存在于服务器上)而不是一个文本文件。与文本文件相比,解析此文件更容易。例如,考虑以下字典:

    NSDictionary *dictionary = @{@"videos"  : @[@"http://youtube.com/abc", @"http://vimeo.com/xyz"],
    @"websites": @[@"http://apple.com", @"http://microsoft.com"]};

    您可以将其保存到 plist 中:

    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"files.plist"];

    [dictionary writeToFile:plistPath atomically:YES];

    您可以将该文件添加到您的包或其他任何东西,然后在未来的某个日期阅读它:

    dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    或者,您可以将其写入 JSON 文件:

    NSError  *error    = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonPath = [documentsPath stringByAppendingPathComponent:@"files.json"];
    [data writeToFile:jsonPath atomically:YES];

    您可以读取该 JSON 文件:

    data       = [NSData dataWithContentsOfFile:jsonPath];
    dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

    无论哪种方式,您都可以像这样获取视频或网站的列表:

    NSArray *videos   = dictionary[@"videos"];
    NSArray *websites = dictionary[@"websites"];
  2. 既然您已经拥有了一系列视频和网站,接下来的问题就是如何使用这些 URL。

    你可以这样做:

    for (NSString *urlString in videos) {
    NSURL *url = [NSURL URLWithString: urlString];
    // now do something with the URL
    }

    最大的问题是“做某事”的逻辑是什么。因为您要处理很多 URL,所以您会希望使用基于 NSOperation 的解决方案,而不是 GCD 解决方案,因为 NSOperationQueue 可以让您控制并发度。我建议使用基于 NSOperation 的网络库,例如 AFNetworking .例如,为您的网站下载 HTML:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 4;

    for (NSString *urlString in websites)
    {
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // convert the `NSData` responseObject to a string, if you want

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

    // now do something with it, like saving it in a cache or persistent storage
    // I'll just log it

    NSLog(@"responseObject string = %@", string);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error = %@", error);
    }];
    [queue addOperation:operation];
    }

    话虽如此,我不确定启动大量网络请求是否有意义。您真的不想等到用户点击其中一个单元格再检索它(例如,然后只需在 UIWebView 中打开该 URL)吗?您不希望应用程序不必要地占用用户的数据计划和电池检索他们可能不想检索的内容。 (Apple 已拒绝从蜂窝连接请求过多数据的应用程序。)或者,至少,如果您想预先检索内容,请仅在需要时检索内容(例如,在 cellForRowAtIndexPath 中),这将检索可见行,而不是 text/plist/json 文件中可能存在的数百行。

坦率地说,我们需要更清楚地说明您要做什么,我们也许可以通过更简洁的建议来帮助您。

关于iOS NSURL 文件多个请求的排队机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19814221/

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