I have a situation where I am lazy to upload images from www.
This is a list of elements, when one element is listened, a detailed view is transferred to the navigation controller.
In this detailed view, the element has an image, which is first the default image, and I want to start downloading the image from the URL.
So, I create an object that, after initialization, separates a new stream, which in turn loads the content, and then notifies my idea that the data is loaded:
- (MyLoader *)initWithUrl:(NSURL *)url requester:(id)requester {
self.url = url;
self.requester = requester;
[self performSelectorInBackground:@selector(loadIt) withObject:nil];
}
- (void)loadIt {
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NSData *data = [NSData dataWithContentsOfURL:url];
[requester performSelectorOnMainThread:@selector(dataReady) withObject:data waitUntilDone:YES;
[arp release];
}
- (void)somewhere {
MyLoader *loader = [[[MyLoader] alloc] initWithUrl:someUrl requester:self] autorelease];
}
A few notes:
, . performSelectorOnMainThread , data requester .
, NSData , withObject:nil. - .
, . waitUntilDone:YES, requester dataReady. performSelectorOnMainThread ( ), dataReady. BTW, - (void)dataReady:(NSData*) int x = 1; ( ). , waitUntilDone:NO, .
( ), .
- , ?
, , , [requester performSelectorOnMainThread..., .
, , .
#0 0x00a71004 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ ()
#1 0x93436e3b in objc_exception_throw ()
#2 0x0028aca6 in __NSThreadPerformPerform ()
#3 0x00a098e1 in CFRunLoopRunSpecific ()
#4 0x00a08c48 in CFRunLoopRunInMode ()
#5 0x0005a78d in GSEventRunModal ()
#6 0x0005a852 in GSEventRun ()
#7 0x0168a003 in UIApplicationMain ()
#8 0x000028d4 in main (argc=1, argv=0xbffff100) at /Users/myName/Document/appName/main.m:14