2016-01-04 76 views
0

的功能在JSONDownload.mNSURLConnection的不下載

-(void)downloadEntries{ 
    NSString *urlString = @"https://itunes.apple.com/in/rss/topmovies/limit=50/genre=4431/json"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    [self.webData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data{ 
    [self.webData appendData:data]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSError *error; 
    self.dictionary = [NSJSONSerialization JSONObjectWithData:self.webData options:0 error:&error]; 
    NSLog(@"%@", [error localizedDescription]); 
} 

-(NSString *)returnName: (NSInteger)index{ 
    NSArray *entry = [self entryArray]; 
    NSDictionary *indexDictionary = [entry objectAtIndex:index]; 
    NSDictionary *nameDictionary = [indexDictionary objectForKey:@"im:name"]; 
    NSString *nameOfMovie = [nameDictionary objectForKey:@"label"]; 
    return nameOfMovie; 
} 

-(NSInteger)numberOfEntries{ 
    NSDictionary *feed = [self dictionary]; 
    NSArray *entry = [feed objectForKey:@"entry"]; 
    return [entry count]; 
} 

其通過另一個類FeedEntry.m稱爲

- (void)recieveEntries{ 
    JSONDownload *download = [JSONDownload sharedInstance]; 
    [download downloadEntries]; // calling method 

    NSInteger numberOfEntries = [download numberOfEntries]; //calling method 

    for (int index = 0; index < numberOfEntries; index++) { 

     self.name = [download returnName:index]; //calling method 
     NSLog(@"%@",self.name); 
    } 
} 

然而關於追蹤我發現connectionDidFinishLoading:不叫,因此字典該函數中的屬性未初始化。因此,我將numberOfEntries設置爲0,並且不執行循環。應該做什麼?

+0

您是否實現了其他(必需的)委託方法'didReceiveResponse'和'didReceiveData'?無論如何,在'downloadEntries'之後調用'numberOfEntries'將總是返回0,因爲連接的異步行爲。 – vadian

+0

@vadian yes我有 – Rishab

+0

您需要一個完成處理程序或在'connectionDidFinishLoading'方法中調用的委託方法來通知'FeedEntry'數據可用。 – vadian

回答

1

您需要一個完成處理程序,因爲NSURLConnection是異步工作的。

FeedEntry.h

定義一個塊類型和一個completion屬性

typedef void (^ConnectionCompletion)(NSDictionary *data, NSError *error); 

@property (nonatomic, copy) ConnectionCompletion completion; 

FeedEntry.m變化downloadEntries

- (void)downloadEntriesWithCompletion:(ConnectionCompletion)completion{ 
    self.completion = completion; 
    NSString *urlString = @"https://itunes.apple.com/in/rss/topmovies/limit=50/genre=4431/json"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

並改變connectionDidFinishLoading

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSError *error; 
    self.dictionary = [NSJSONSerialization JSONObjectWithData:self.webData options:0 error:&error]; 
    self.completion(self.dictionary, error); 
} 

的完成處理程序返回dictio中以及潛在的序列化錯誤。

現在打電話與

JSONDownload *download = [JSONDownload sharedInstance]; 
[download downloadEntriesWithCompletion:^(NSDictionary *data, NSError *error) { 
    if (error) { 
     NSLog(@"%@", error); 
    } else { 
     NSInteger numberOfEntries = [download numberOfEntries]; //calling method 
     for (int index = 0; index < numberOfEntries; index++) { 
     self.name = [download returnName:index]; //calling method 
     NSLog(@"%@",self.artist); 
     } 
    } 
}]; 

在完成處理程序返回的參數的方法只是一個例子。你也可以傳遞共享實例或任何你需要的。

您還應該執行connectionDidFailWithError並在那裏調用完成處理程序返回nil和錯誤。

+0

我剛剛意識到我必須學習如此多的關於塊。感謝代碼。它似乎現在工作。然而'didReceiveData:'顯示0個鍵/值對,而在'connectionDidFinishLoading:'它拋出一個異常說'數據參數爲空'。我檢查了語法,但一切似乎都按照正確的順序。 – Rishab

+0

是'webData'初始化爲'[NSMutabledata data]'? – vadian

+0

哦,對...是的,它現在消失了。 – Rishab