简体   繁体   English

UIView从superview中删除很慢

[英]UIView is slow to be removed from superview

I'm adding a UIView to the window while some threaded background updates are happening, then use a delegate method to remove the view. 我正在向窗口添加UIView,同时发生一些线程后台更新,然后使用委托方法删除视图。 Everything is happening as intended, but the view remains for several seconds after hideActivityViewer is called. 一切都按预期发生,但在调用hideActivityViewer后,视图仍会持续几秒钟。 Not sure if it matters, but the app uses a UITabBarController. 不确定是否重要,但应用程序使用UITabBarController。

The updating methods were in a separate class but are currently in AppDelegate.m for debugging purposes. 更新方法在一个单独的类中,但目前在AppDelegate.m中用于调试目的。 As I said, everything works. 正如我所说,一切正常。 When the updates are complete, it writes "Foo" to the log, but the view persists for several seconds. 更新完成后,它会将“Foo”写入日志,但视图会持续几秒钟。 Any help would be appreciated. 任何帮助,将不胜感激。 Superfluous code has been omitted: 多余的代码已被省略:

AppDelegate.h AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
  UIView *activityView;
  id _delegate;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
- (void)showActivityViewer;
- (void)updateComplete;
- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate;
- (id)delegate;
- (void)setDelegate:(id)new_delegate;
@end

AppDelegate.m AppDelegate.m

- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate {
  [self setDelegate:aDelegate];
  NSOperationQueue *queue = [NSOperationQueue new];
  NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateRemoteDataWithDelegate:) object:aDelegate];
  [queue addOperation:operation];
  [operation release];
}

- (void)updateRemoteDataWithDelegate:(id)aDelegate {
  [self setDelegate:aDelegate];
  ...do stuff...
  if ([_delegate respondsToSelector:@selector(updateComplete)]) {
    [_delegate updateComplete];
  } else { 
    [NSException raise:NSInternalInconsistencyException format:@"Delegate doesn't respond to updateComplete"];
  }
}

-(void)showActivityViewer {
  [activityView release];
  activityView = [[UIView alloc] initWithFrame: CGRectMake(window.bounds.size.width-50, 60, 50, 50)];
  ...formatting...
  [window addSubview: activityView];
  [activityView release];
}
-(void)hideActivityViewer {
  [activityView removeFromSuperview];
  activityView = nil;
  NSLog(@"Foo");
}

- (id)delegate {
  return _delegate;
}

- (void)setDelegate:(id)new_delegate {
  _delegate = new_delegate;
}

It looks to me like you are causing a thread to call a delegate method which does stuff in UIView - you can't do that since UIView is not thread-safe. 在我看来,你正在使一个线程调用一个在UIView中执行操作的委托方法 - 你不能这样做,因为UIView不是线程安全的。

Use performSelectorOnMainThread to do this safely - your delegate method could call another method on the main thread this way. 使用performSelectorOnMainThread安全地执行此操作 - 您的委托方法可以通过这种方式调用主线程上的另一个方法。

UI operations should be done on the main thread; UI操作应该在主线程上完成; your example pushes UIView onto a separate thread, which is not recommended. 您的示例将UIView推送到单独的线程,不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM