我正在創建FlickrImage類的實例,解析Flickr API照片響應。這個類有,做另一個API調用來獲取地理定位的方法的getLocation:ObjC委託方法永遠不會被調用
NSLog(@"getting location for %i",self.ID);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
OFFlickrAPIRequest *flickrAPIRequest = [[OFFlickrAPIRequest alloc] initWithAPIContext[appDelegate sharedDelegate].flickrAPIContext];
[flickrAPIRequest setDelegate:self];
NSString *flickrAPIMethodToCall = @"flickr.photos.geo.getLocation";
NSDictionary *requestArguments = [[NSDictionary alloc] initWithObjectsAndKeys:FLICKR_API_KEY,@"api_key",self.ID,@"photo_id",nil];
[flickrAPIRequest callAPIMethodWithGET:flickrAPIMethodToCall arguments:requestArguments];
[pool release];
我已經實現,將趕上來自API的響應,並與地理定位數據更新FlickrImage實例的回調方法 - 但它從來沒有被調用。下面是其中的情況下獲得創建:
NSDictionary *photosDictionary = [inResponseDictionary valueForKeyPath:@"photos.photo"];
NSDictionary *photoDictionary;
FlickrImage *flickrImage;
for (photoDictionary in photosDictionary) {
flickrImage = [[FlickrImage alloc] init];
flickrImage.thumbnailURL = [[appDelegate sharedDelegate].flickrAPIContext photoSourceURLFromDictionary:photoDictionary size:OFFlickrThumbnailSize];
flickrImage.hasLocation = TRUE; // TODO this is actually to be determined...
flickrImage.ID = [NSString stringWithFormat:@"%@",[photoDictionary valueForKeyPath:@"id"]];
flickrImage.owner = [photoDictionary valueForKeyPath:@"owner"];
flickrImage.title = [photoDictionary valueForKeyPath:@"title"];
[self.flickrImages addObject:[flickrImage retain]];
[flickrImage release];
[photoDictionary release];
}
的retain
是那裏,因爲我認爲這可能有助於解決這一點,但它沒有 - 也確實不是NSMutableArray裏(flickrImages是NSMutableArray的)反正保留其成員?
編輯我應該補充的是,getLocation
方法(第一代碼段)在一個線程被推出: [NSThread detachNewThreadSelector:@selector(的getLocation)toTarget:自withObject:無];
您應該刪除for循環中額外的'retain'調用。該數組確實保留其成員,所以您基本上正在創建內存泄漏。 – benzado 2009-12-11 20:26:41