1

我需要从 JSON 下载数据,并将数据分配NSDataNSOperationQueue. 这是我的代码:

-(void)parsingInfo {
    NSURL *url = [NSURL URLWithString:@"http://someJSON.json"];
    NSData *data;

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){

        if(error)
        {
            // Error Downloading data
            NSLog(@"Error");
        }
        else
        {
            data = jsonData;
        }
    }];

    if (data) {
        NSError *error;
        NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

        application = [JSONDic objectForKey:@"Applications"];
        NSArray *featured = [JSONDic objectForKey:@"Featured"];
        NSDictionary *dict2;
        dict2 = [featured objectAtIndex:0];

    } else {

        NSLog(@"Error, no data!");
    }
}
4

2 回答 2

2

将一个块传递给您的 NSOperation,该操作可以使用 NSData 对象作为参数调用该块。

于 2013-10-18T19:16:12.890 回答
-1

如果您想在到达“if (data)”语句之前等待“数据”被填充,那么要么.. a) 进行同步请求调用。

  + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

或者,

b)用户信号量阻塞/中断该线程,直到您收到数据。

-(void)parsingInfo
{
            NSURL *url = [NSURL URLWithString:@http://someJSON.json];
            __block NSData *data;

                // Create a semaphore to block this thread ( or run loop) until we get response back from server.
            dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);

            [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:downloadQueue completionHandler:^(NSURLResponse* response, NSData* jsonData, NSError* error){

                if(error)
                {
                    // Error Downloading data
                    NSLog(@"Error");
                }
                else
                {
                    data = jsonData;
                }

               // Signal to release the semaphore (i.e. unblock the current thread).
                dispatch_semaphore_signal(waitSemaphore);

            }];
                // A dispatch semaphore is an efficient implementation of a traditional counting semaphore. Dispatch semaphores call down
            // to the kernel only when the calling thread needs to be blocked. If the calling semaphore does not need to block, no kernel call is made.
            while (dispatch_semaphore_wait(waitSemaphore, DISPATCH_TIME_NOW))
            {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
            }

            // Release the semaphore.
            dispatch_release(waitSemaphore);

            if (data) {
                NSError *error;
                NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

                application = [JSONDic objectForKey:@"Applications"];
                NSArray *featured = [JSONDic objectForKey:@"Featured"];
                NSDictionary *dict2;
                dict2 = [featured objectAtIndex:0];

            } else {

                NSLog(@"Error, no data!");
            }

}

随意问任何问题。

于 2013-10-18T19:42:35.633 回答