Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 数据解析的多线程问题_Ios_Multithreading - Fatal编程技术网

Ios 数据解析的多线程问题

Ios 数据解析的多线程问题,ios,multithreading,Ios,Multithreading,我开始了一个项目来练习从Flickr解析JSON并在我的应用程序中展示照片。除了我尝试对应用程序的下载部分进行多线程处理外,其他一切都可以完美运行。我收到错误消息: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds for empty array' *** First

我开始了一个项目来练习从Flickr解析JSON并在我的应用程序中展示照片。除了我尝试对应用程序的下载部分进行多线程处理外,其他一切都可以完美运行。我收到错误消息:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds for empty array'
*** First throw call stack:
(0x1c94012 0x10d1e7e 0x1c360b4 0x397f 0xf9103 0xf94df 0xfac4c 0xf97ef 0x2c2c0 0x2c245 0x2c095 0x1c5cafe 0x1c5ca3d 0x1c3a7c2 0x1c39f44 0x1c39e1b 0x1bee7e3 0x1bee668 0x15ffc 0x257d 0x24a5)
libc++abi.dylib: terminate called throwing an exception
这是我尝试执行的代码:

-(void)loadFlickrPhotos {
    self.photoURLS = [[NSMutableArray alloc]init];
    self.photoTitles = [[NSMutableArray alloc]init];

    //Creating second thread here
    dispatch_queue_t downloadQ = dispatch_queue_create("Picture Download", NULL);
    dispatch_async(downloadQ, ^{

    NSString* urlstring = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?format=json&sort=random&method=flickr.photos.search&tags=rocket&tag_mode=all&api_key=0e2b6aaf8a6901c264acb91f151a3350&nojsoncallback=1"];
    NSURL* url = [[NSURL alloc]initWithString:urlstring];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSArray* photos = [[parsedObject valueForKey:@"photos"]objectForKey:@"photo"];

       for(NSDictionary* photo in photos) {

         NSString* photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg", [photo objectForKey:@"farm"],[photo objectForKey:@"server"],[photo objectForKey:@"id"],[photo objectForKey:@"secret"]];

         NSString* photoTitleString = [photo objectForKey:@"title"];

         [self.photoURLS addObject:[NSURL URLWithString:photoURLString]];
         [self.photoTitles addObject:photoTitleString];
      }
         NSLog(@"Photo #: %i", self.photoURLS.count);
    }); //End of Second Thread
}

self.photoURL和self.photoTitles可能不是线程安全属性

在主线程中执行此操作2行

[self.photoURLS addObject:[NSURL URLWithString:photoURLString]];
[self.photoTitles addObject:photoTitleString];

如果您指出导致异常的确切行,将会有所帮助。它似乎不是你发布的任何代码。我在我的代码中间添加了DexChyQueReWixMistq,里面有[Self.PopulsAdObj...…]。这就是您的想法吗?dispatch_async(dispatch_get_main_queue(),^{[self.photoURLS addObject:[NSURL URLWithString:photoURLString];[self.photoTitles addObject:photoTitleString];});是的,我试过了,但还是一样。我现在所做的是尝试将循环的代码部分与方法的其余部分分开。如果您的属性被定义为非原子的,并且您试图从线程主线程访问它,那么您不能保证结果。为什么特别是“主线程”,因为它们是在主线程中创建和访问的。不,我使用CS193p作为一些指导,但是我自己在做。只是想练习一下多线程和解析。那么你的意思是我应该尝试将我的photourl和title数组更改为非原子数组?