我刚刚在我的应用程序中引入了多线程,以获得一个愚蠢的UIActivityIndicatorView。好吧,活动指示器工作正常 - 但现在我的应用程序有时会崩溃,有时它不会 - 在其他条件控制的情况下......我需要弄清楚这一点,但不知道从哪里开始寻找......
那么,初学者在iPhone上使用多线程经常犯的常见错误是什么?请具体说明您的答案。谢谢你的时间。
更新:我添加了有问题的来源以供参考。
//--------------------Where the multithreading starts------------------------
-(IBAction)processEdits:(id)sender
{
//Try to disable the UI to prevent user from launching duplicate threads
[self.view setUserInteractionEnabled:NO];
//Initialize indicator (delcared in .h)
myIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(155, 230, 20, 20)];
myIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[self.view addSubview:myIndicator];
[self.view bringSubviewToFront:myIndicator];
[myIndicator startAnimating];
//Prepare and set properties of the NEXT modal view controller to switch to
controller = [[EndViewController alloc] initWithNibName:@"EndViewController" bundle:nil];
controller.delegate = self;
[self performSelectorInBackground:@selector(threadWork:) withObject:nil];
}
//-----------------------------THE THREAD WORK--------------------------------
-(IBAction)threadWork:(id)sender{
NSAutoreleasePool * pool;
NSString * status;
pool = [[NSAutoreleasePool alloc] init];
assert(pool != nil);
//The image processing work that takes time
controller.photoImage = [self buildPhoto];
//Stop the UIActivityIndicatorView and launch next modal view
[self performSelectorOnMainThread:@selector(stopSpinner:)withObject:nil waitUntilDone:NO];
[pool drain];
}
//-------------------Most of the WORKLOAD called in above thread ------------------------
-(UIImage*)buildPhoto
{
/*
This is the work performed in the background thread. Process photos that the user has edited and arrange them into a UIView to be finally flattened out into a new UIImage. Problem: UI usually changes for some reason during this work.
*/
UIView* photoContainerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,975,1300)];
photoContainerView.backgroundColor = [UIColor whiteColor];
UIImage* purikuraFlattened;
int spacerX = 10;
int spacerY = 10;
switch (myPattern) {
case 0:
photoContainerView.frame = CGRectMake(0, 0, 320, 427);
layoutSingle = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x,photoContainerView.frame.origin.y,320,427)];
[photoContainerView addSubview:layoutSingle];
layoutSingle.image = editPhotoData1;
break;
case 1:
layoutAimg1 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX, photoContainerView.frame.origin.y+spacerY, 427, 320)];
layoutAimg2 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX+427, photoContainerView.frame.origin.y+spacerY, 427, 320)];
layoutAimg3 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX, photoContainerView.frame.origin.y+spacerY+320, 427, 320)];
layoutAimg4 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX+427, photoContainerView.frame.origin.y+spacerY+320, 427, 320)];
layoutAimg5 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX, photoContainerView.frame.origin.y+spacerY+(320*2), 427, 320)];
layoutAimg6 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX+427, photoContainerView.frame.origin.y+spacerY+(320*2), 427, 320)];
layoutAimg7 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX, photoContainerView.frame.origin.y+spacerY+(320*3), 427, 320)];
layoutAimg8 = [[UIImageView alloc] initWithFrame:CGRectMake(photoContainerView.frame.origin.x+spacerX+427, photoContainerView.frame.origin.y+spacerY+(320*3), 427, 320)];
[photoContainerView addSubview:layoutAimg1];
[photoContainerView addSubview:layoutAimg2];
[photoContainerView addSubview:layoutAimg3];
[photoContainerView addSubview:layoutAimg4];
[photoContainerView addSubview:layoutAimg5];
[photoContainerView addSubview:layoutAimg6];
[photoContainerView addSubview:layoutAimg7];
[photoContainerView addSubview:layoutAimg8];
if(myShots == 1){
rotPhoto1 = [self rotateImage:editPhotoData1.size:editPhotoData1];
layoutAimg1.image = rotPhoto1;
layoutAimg2.image = rotPhoto1;
layoutAimg3.image = rotPhoto1;
layoutAimg4.image = rotPhoto1;
layoutAimg5.image = rotPhoto1;
layoutAimg6.image = rotPhoto1;
layoutAimg7.image = rotPhoto1;
layoutAimg8.image = rotPhoto1;
}else if(myShots == 2){
rotPhoto1 = [self rotateImage:editPhotoData1.size: editPhotoData1];
rotPhoto2 = [self rotateImage:editPhotoData2.size: editPhotoData2];
layoutAimg1.image = rotPhoto1;
layoutAimg2.image = rotPhoto2;
layoutAimg3.image = rotPhoto2;
layoutAimg4.image = rotPhoto1;
layoutAimg5.image = rotPhoto1;
layoutAimg6.image = rotPhoto2;
layoutAimg7.image = rotPhoto2;
layoutAimg8.image = rotPhoto1;
}else if(myShots == 4){
rotPhoto1 = [self rotateImage:editPhotoData1.size: editPhotoData1];
rotPhoto2 = [self rotateImage:editPhotoData2.size: editPhotoData2];
rotPhoto3 = [self rotateImage:editPhotoData3.size: editPhotoData3];
rotPhoto4 = [self rotateImage:editPhotoData4.size: editPhotoData4];
layoutAimg1.image = rotPhoto1;
layoutAimg2.image = rotPhoto2;
layoutAimg3.image = rotPhoto3;
layoutAimg4.image = rotPhoto4;
layoutAimg5.image = rotPhoto1;
layoutAimg6.image = rotPhoto2;
layoutAimg7.image = rotPhoto3;
layoutAimg8.image = rotPhoto4;
}
break;
}
UIGraphicsBeginImageContext(photoContainerView.bounds.size);
[purikuraContainerView.layer renderInContext:UIGraphicsGetCurrentContext()];
photoFlattened = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSEnumerator *enumerator = [[photoContainerView subviews] objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[object removeFromSuperview];
}
[photoContainerView release];
photoContainerView = nil;
if(rotPhoto1 != nil){
[rotPhoto1 release];
rotPhoto1 = nil;
}
if(rotPhoto2 != nil){
[rotPhoto2 release];
rotPhoto2 = nil;
}
if(rotPhoto3 != nil){
[rotPhoto3 release];
rotPhoto3 = nil;
}
if(rotPhoto4 != nil){
[rotPhoto4 release];
rotPhoto4 = nil;
}
if(rotPhotoSm1 != nil){
[rotPhotoSm1 release];
rotPhotoSm1 = nil;
}
if(rotPhotoSm2 != nil){
[rotPhotoSm2 release];
rotPhotoSm2 = nil;
}
if(rotPhotoSm3 != nil){
[rotPhotoSm3 release];
rotPhotoSm3 = nil;
}
if(rotPhotoSm4 != nil){
[rotPhotoSm4 release];
rotPhotoSm4 = nil;
}
return photoFlattened;
}
//-----------------------------STOP THE UIACTIVITYINDICATORVIEW---------------------
-(IBAction)stopSpinner:(id)sender
{
[self.view setUserInteractionEnabled:YES];
[myIndicator stopAnimating];
[myIndicator release];
myIndicator = nil;
if(myPattern == 0){
NSLog(@"SINGLE-SHOT MODE");
controller.isSingleShot = TRUE;
}else{
NSLog(@"MULTI-SHOT MODE");
controller.isSingleShot = FALSE;
}
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
[allStamps removeAllObjects];
[imageFrames removeAllObjects];
switch (myShots) {
case 1:
[editPhotoData1 release];
break;
case 2:
[editPhotoData1 release];
[editPhotoData2 release];
break;
case 4:
[editPhotoData1 release];
[editPhotoData2 release];
[editPhotoData3 release];
[editPhotoData4 release];
break;
}
/* This is the edited photo that has been onscreen. Processing is now done so it is okay to release it. The UI should be updated and now have a blank, black background instead of the image.
*/
editedPhoto.image = nil;
[editedPhoto release];
editedPhoto = nil;
}
答案 0 :(得分:15)
这个问题在Cocoa多线程上有一些很好的资源:"Where can I find a good tutorial on iPhone/Objective c multithreading?"
我还强烈建议阅读新的Concurrency Programming Guide(但是,忽略块和调度队列,因为Grand Central Dispatch尚未在iPhone OS上提供 iOS 4.0只添加了块和GCD ),因为它强有力地使用NSOperation
和NSOperationQueue
等结构作为手动创建线程的替代方案。有关手动创建的线程的信息,请参阅Threading Programming Guide。
正如RC所提到的,使用多线程Cocoa应用程序崩溃的最大单一来源是同时访问共享资源。 @synchronized
指令不是最快的pointed out by Colin Wheeler,因此您可能希望使用NSLock
来保护对共享资源的访问权限。但是,任何类型的锁定都可能很昂贵,这就是为什么我一直在将我的应用程序迁移到使用单宽NSOperationQueues
来访问这些资源。性能的提升非常显着。
Cocoa和多线程的另一个问题是来自用户界面更新。 Cocoa中的所有UI更新必须在主线程上完成,否则可能导致不稳定。如果您有后台线程执行计算,请确保在-performSelectorOnMainThread:withObject:waitUntilDone:
方法调用中包装任何方法更新UI。
答案 1 :(得分:5)
在使用线程时,初学者(使用任何语言)最常见的错误可能是允许访问可变共享资源而不需要保护/互斥。你保护资源,如:
@synchronized(sharedData) { // modify sharedData safely }
您需要限制线程之间共享的数据量,如果必须共享,则首选不可变对象以减少同步引起的争用。
管理线程是另一个可能出现问题的地方。这是一个特定于iPhone中线程使用的文档参考。
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html。
在没有提供代码的情况下,任何人都猜测你的应用程序出了什么问题,但我首先要确保你正确地管理线程的创建和终止,并特别注意线程尝试的任何共享资源。访问。