Skip to content

Commit

Permalink
Adding in some functionality and utilities towards ensuring that deal…
Browse files Browse the repository at this point in the history
…ings with TiUIView and children can be done on only the main thread.
  • Loading branch information
Blain Hamon authored and Blain Hamon committed Aug 4, 2010
1 parent ee3dcd3 commit f859d42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions iphone/Classes/TiAction.h
Expand Up @@ -21,4 +21,9 @@
-(id)initWithTarget:(id)target_ selector:(SEL)selector_ arg:(id)arg_;
-(void)execute;

@property(nonatomic,readonly) id target;
@property(nonatomic,readonly) SEL selector;
@property(nonatomic,readonly) id arg;


@end
1 change: 1 addition & 0 deletions iphone/Classes/TiAction.m
Expand Up @@ -9,6 +9,7 @@


@implementation TiAction
@synthesize target,selector,arg;

-(id)initWithTarget:(id)target_ selector:(SEL)selector_ arg:(id)arg_
{
Expand Down
10 changes: 10 additions & 0 deletions iphone/Classes/TiBase.h
Expand Up @@ -366,11 +366,21 @@ enum {
#ifdef DEBUG
#define FRAME_DEBUG(f) \
NSLog(@"FRAME -- size=%fx%f, origin=%f,%f",f.size.width,f.size.height,f.origin.x,f.origin.y);

#define WARN_IF_BACKGROUND_THREAD \
if(![NSThread isMainThread]) \
{ \
NSLog(@"[WARN] %@%@ was not running on the main thread.",NSStringFromClass([self class]),CODELOCATION); \
} \

#else
#define FRAME_DEBUG(f)

#define CHECK_MAIN_THREAD
#endif



#define DEFINE_DEF_PROP(name,defval)\
-(id)name \
{\
Expand Down
4 changes: 4 additions & 0 deletions iphone/Classes/TiViewProxy.h
Expand Up @@ -123,8 +123,11 @@
-(BOOL)isAutoHeightOrWidth;
-(BOOL)canHaveControllerParent;

-(void)makeViewPerformSelector:(SEL)selector withObject:(id)object createIfNeeded:(BOOL)create waitUntilDone:(BOOL)wait;

@end


#define USE_VIEW_FOR_METHOD(resultType,methodname,inputType) \
-(resultType) methodname: (inputType)value \
{ \
Expand All @@ -141,6 +144,7 @@
}



#define USE_VIEW_FOR_VERIFY_WIDTH USE_VIEW_FOR_METHOD(CGFloat,verifyWidth,CGFloat)
#define USE_VIEW_FOR_VERIFY_HEIGHT USE_VIEW_FOR_METHOD(CGFloat,verifyHeight,CGFloat)
#define USE_VIEW_FOR_AUTO_WIDTH USE_VIEW_FOR_METHOD(CGFloat,autoWidthForWidth,CGFloat)
Expand Down
39 changes: 39 additions & 0 deletions iphone/Classes/TiViewProxy.m
Expand Up @@ -12,6 +12,8 @@
#import "TiRect.h"
#import "TiLayoutQueue.h"

#import "TiAction.h"

#import <QuartzCore/QuartzCore.h>
#import <libkern/OSAtomic.h>
#import <pthread.h>
Expand Down Expand Up @@ -586,6 +588,13 @@ -(TiUIView*)view
{
if (view == nil)
{
WARN_IF_BACKGROUND_THREAD
#ifdef DEBUG
if(![NSThread isMainThread])
{
NSLog(@"[WARN] Break here");
}
#endif
[self viewWillAttach];

// on open we need to create a new view
Expand Down Expand Up @@ -1317,5 +1326,35 @@ -(void)setCenter:(id)value
[self setNeedsReposition];
}

-(void)makeViewPerformAction:(TiAction *)action
{
[[self view] performSelector:[action selector] withObject:[action arg]];
}

-(void)makeViewPerformSelector:(SEL)selector withObject:(id)object createIfNeeded:(BOOL)create waitUntilDone:(BOOL)wait
{
BOOL isAttached = [self viewAttached];

if(!isAttached && !create)
{
return;
}

if([NSThread isMainThread])
{
[[self view] performSelector:selector withObject:object];
return;
}

if(isAttached)
{
[[self view] performSelectorOnMainThread:selector withObject:object waitUntilDone:wait];
return;
}

TiAction * ourAction = [[TiAction alloc] initWithTarget:nil selector:selector arg:object];
[self performSelectorOnMainThread:@selector(makeViewPerformAction:) withObject:ourAction waitUntilDone:wait];
[ourAction release];
}

@end

2 comments on commit f859d42

@lukemelia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this commit, my app is logging lots of warnings (TiViewProxy.m:593) was not running on the main thread. Is this indicative of something I'm doing wrong in javascript-land?

@appcelerator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK (well, it's not). We simply are trying to track down issues that are a side effect of not being on the UI thread. we'll fix the issues related to the warning before the next release. These aren't app issues.

Please sign in to comment.