开发者

Crash in background thread in UIScrollview

开发者 https://www.devze.com 2023-02-14 22:56 出处:网络
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 th

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

EDIT:

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];
}

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.

The debugger says the crash is at obj_msgSend after the if check in the loadpages.

EDIT: 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  0x3开发者_开发百科14d5cb8 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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号