UIScrollview中的后台线程崩溃 [英] Crash in background thread in UIScrollview
问题描述
为了提高性能,我在后台线程中为滚动视图创建了子视图,然后使用performSelectorOnMainThread
将事件发布到主线程,并将视图添加到其中的滚动视图中并带上字体.但是有时我会崩溃.
In order to improve performance i am creating subviews for my scroll view in a background thread and then posting the event to main thread using performSelectorOnMainThread
and adding the view into the scrollview in it and bring it to font. However i get a crash sometimes.
当我在后台线程中对此进行检查时.
When i check this in my background thread.
if( nil == myView.view.superview)
如何在后台线程中安全地执行此检查线程.如果我必须在主线程上执行,那么我需要进行很多更改设计,因此我可以将其发布到主线程并使用返回值吗? 我对其他选择持开放态度.
How can i perform this check thread safe in the background thread. If i have to perform on main thread then i need to change the design a lot, hence can there be a way i post it to main thread and use the return value. I am open to alternatives on the same.
TIA,
Praveen S
Praveen S
我已经实现了分页的UIScrollview.但是,由于性能问题以及为了提高UI的响应性,我将使用创建的子视图移动到另一个线程中
I have implemented UIScrollview with paging. However due to performance issues and to improve responsiveness of the UI i moved creating the subviews into a different thread using
[self performSelectorInBackground:@selector(loadPages:) withObject:nil];
在加载页面中,我执行以下操作
In load pages, i do the following
if( nil == myView.view.superview)
{
Create the view
[[self performSelectorOnMainThread:@selector(refreshViews:) myView.view waitUntilDone:YES];
}
在刷新视图中,我将子视图添加到scrollview并将其置于最前面.
In refresh view i add the subview to scrollview and bring it to front.
[myScrollView addSubview:views];
[myScrollView bringSubviewToFront:views];
但是,添加和页面控件可以正常工作.滚动还可以,还可以.但是当我快速滚动它(以快速方式在屏幕上滑动)时,应用程序崩溃了.
However the get added and page control works fine. Scrolling is ok ok kind smooth. But when i scroll it fast( swipe across the screen in a fast manner) the application crashes.
调试器说,如果在加载页面中执行了if检查,则崩溃发生在obj_msgSend.
The debugger says the crash is at obj_msgSend after the if check in the loadpages.
堆栈跟踪-这次它在检查@synchronized时崩溃了.出于安全原因,我已经编辑了崩溃日志以删除项目的特定名称,但是跟踪如下.
Stack trace -- Well it crashed at the check @synchronized this time. I have edited the crash log to remove project specific names for security reasons but the trace is as below.
#0 0x33a06464 in objc_msgSend ()
#1 0x314d6984 in -[UIViewController _loadViewFromNibNamed:bundle:] ()
#2 0x314d5cb8 in -[UIViewController loadView] ()
#3 0x313d6226 in -[UIViewController view] ()
#6 0x00009920 in -[myViewController loadPages:] (self=0x12a2a0, _cmd=0x1eac1, abcd=0x0) at
#7 0x302d9198 in -[NSThread main] ()
#8 0x302d2248 in __NSThread__main__ ()
#9 0x3110c88c in _pthread_start ()
#10 0x31101a90 in thread_start ()
推荐答案
UIKit并非线程安全的,除非另有明确标记.不支持在后台创建视图(和加载笔尖).任何涉及UIView的事情都需要在主线程上进行.即使在将视图添加到超级视图之前,这也适用.
UIKit is not thread-safe unless specifically marked otherwise. Creating views (and loading nibs) in the background is not supported; anything that touches a UIView needs to happen on the main thread. This applies even before your view has been added to a superview.
这篇关于UIScrollview中的后台线程崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!