라고 점점되지 않습니다 내 중첩 된 블록을 살펴 가지고하시기 바랍니다 : 아래의 블록을 가지고,중첩 completionBlock는 objc
- (void)getVideoList:(NSDictionary*)videoData
completionBlock:(void (^)(NSMutableArray *))
completionBlock {
NSArray *videos = (NSArray*)[videoData objectForKey:@"items"];
NSMutableArray* videoList = [[NSMutableArray alloc] init];
for (NSDictionary *videoDetail in videos) {
if (videoDetail[@"id"][@"videoId"]){
[self initializeDictionary:videoDetail completionBlock:^(YoutubeVideo * utubeVideo) {
[videoList addObject:utubeVideo];
// NSLog(@"zuuudo %@", utubeVideo.channelProfileImageURL);
}];
}
}
completionBlock(videoList);
}
- (void)initializeDictionary:(NSDictionary *)dictionary completionBlock:(void (^)(YoutubeVideo *))
completionBlock {
YoutubeVideo *youtubeVideo = [[YoutubeVideo alloc] init];
youtubeVideo.videoTitle = dictionary[@"snippet"][@"title"];
youtubeVideo.videoID = dictionary[@"id"][@"videoId"];
youtubeVideo.channelID = dictionary[@"snippet"][@"channelId"];
[self getChannelProfilePictureForChannelID:youtubeVideo.channelID completionBlock:^(NSMutableArray *channelList) {
NSLog(@"[channelList objectAtIndex:0] %@", [channelList objectAtIndex:0]);
youtubeVideo.channelProfileImageURL = [channelList objectAtIndex:0];
}];
youtubeVideo.channelTitle = dictionary[@"snippet"][@"channelTitle"];
youtubeVideo.videoDescription = dictionary[@"snippet"][@"description"];
youtubeVideo.pubDate = [self dateWithJSONString:dictionary[@"snippet"][@"publishedAt"]];
youtubeVideo.thumbnailURL = dictionary[@"snippet"][@"thumbnails"]
[@"high"][@"url"];
completionBlock(youtubeVideo);
}
- (void)getChannelProfilePictureForChannelID:(NSString*)channelID completionBlock:(void (^)(NSMutableArray *))completionBlock
{
NSString *URL = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/channels?part=snippet&fields=items/snippet/thumbnails/default&id=%@&key=%@", channelID, apiKey];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[URL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (!error){
[self getChannelProfileImageList:[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] completionBlock:
^(NSMutableArray * channelList) {
// return the final list
completionBlock(channelList);
}];
}
else {
// TODO: better error handling
NSLog(@"error = %@", error);
}
}] resume];
}
- (void)getChannelProfileImageList:(NSDictionary*)channelData
completionBlock:(void (^)(NSMutableArray *))
completionBlock {
NSArray *channels = (NSArray*)[channelData objectForKey:@"items"];
NSMutableArray *channelList = [[NSMutableArray alloc] init];
for (NSDictionary *channelDetail in channels) {
[self initializeDictionaryForChannelProfileImage:channelDetail completionBlock:^(NSString *chnlProfileImageURL) {
[channelList addObject:chnlProfileImageURL];
}];
//[channelList addObject:[self initializeDictionaryForChannelProfileImage:channelDetail]];
//[channelList addObject:[[YoutubeVideo alloc] initWithDictionaryForChannelProfileImage:channelDetail]];
}
completionBlock(channelList);
}
- (void)initializeDictionaryForChannelProfileImage:(NSDictionary *)dictionary completionBlock:(void (^)(NSString *))
completionBlock
{
_channelProfileImageURL = dictionary[@"snippet"][@"thumbnails"]
[@"default"][@"url"];
completionBlock(_channelProfileImageURL);
}
문제는이 - (void)initializeDictionary:(NSDictionary *)dictionary completionBlock:(void (^)(YoutubeVideo *)) completionBlock { }
블록입니다
[self getChannelProfilePictureForChannelID:youtubeVideo.channelID completionBlock:^(NSMutableArray *channelList) { NSLog(@"[channelList objectAtIndex:0] %@", [channelList objectAtIndex:0]); youtubeVideo.channelProfileImageURL = [channelList objectAtIndex:0]; }];
경우 이 코드 행은 블록 반환 값 NSSting
값이 실행될 때 실행되지 않습니다.
youtubeVideo.channelProfileImageURL = _channelProfileImageURL;
NSLog(@"youtubeVideo.channelProfileImageURL %@", youtubeVideo.channelProfileImageURL);
는 코드의 나머지 부분을 실행 한 후 호출지고 :
youtubeVideo.channelTitle = dictionary[@"snippet"][@"channelTitle"];
youtubeVideo.videoDescription = dictionary[@"snippet"][@"description"];
youtubeVideo.pubDate = [self dateWithJSONString:dictionary[@"snippet"][@"publishedAt"]];
youtubeVideo.thumbnailURL = dictionary[@"snippet"][@"thumbnails"]
[@"high"][@"url"];
그래서 값이 내 개체 모델에 삽입되지 않습니다. 제게 제안 해주세요. 미리 감사드립니다.
좋은 하루 보내십시오. 당신은 코드가 실행될 것이라는 기대 비동기 실행을 혼합하는
귀하의 명확하고 명확한 설명에 대해 감사드립니다. 이해하기 쉬웠다. 하지만 모든 것은 4 개의 실행을 동기화 한 후에도 여전히 [self getChannelProfilePictureForChannelID'가 호출됩니다. 내 말은 이미'completionBlock (youtubeVideo); '를 반환한다는 것입니다. 내가'getChannelProfilePictureForChannelID'에서 발견 한 또 다른 사실은 [weakSelf getChannelProfileImageList : [NSJSONSerialization JSONObjectWithData : data options : 0 error : nil] completionBlock :''completionBlock'이 그때 호출하지 않았다는 것입니다. 이것은'initializeDictionary'에서 모든 메소드를 끝내고 호출됩니다. – Tulon
그렇다면 어떻게하면 더 이상 'completionBlock'을'getChannelProfilePictureForChannelID'에서부터 asyncize, 즉 동기화 방식으로 asyncize라고 부를 수 있을까요? – Tulon
@Tulon - 추가 정보가 추가되었습니다. – CRD