[TIMOB-7523] iOS: Use blocks and remove "return caches" from code
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2012-02-14T23:29:01.000+0000 |
Affected Version/s | Release 1.8.0.1 |
Fix Version/s | Sprint 2012-03, Release 2.0.0, Release 1.8.2 |
Components | iOS |
Labels | n/a |
Reporter | Stephen Tramer |
Assignee | Blain Hamon |
Created | 2012-02-02T13:59:50.000+0000 |
Updated | 2012-02-14T23:29:01.000+0000 |
Description
Throughout the code there are functions which need to be performed on the main thread, but still return a value (mostly in Contacts and Media, although there may be others). Now that we have blocks, we can instead use local variables with the
__block
specifier and TiThreadPerformOnMainThread
to clean this up. Using the return cache leads (and has always led to) overretaining memory.
Blain's fix for TIMOB-1542 demonstrates a reasonable pattern for this, but rather than checking [NSThread isMainThread]
and then calling back out to the function, we should instead be putting main thread critical sections within the block itself, i.e.
-(id)getPropertyFromMainThread
{
__block id result = nil;
TiThreadPerformOnMainThread(^{
// Critical code to get value for 'result' here...
[result retain];
}, YES);
return [result autorelease];
}
Note the retain/autorelease
pattern being necessary, as if the block is performed on the main thread, it sets up its own autorelease pool and as a result may point back to a bad memory block.
The pull request for 1542 will include the commits fixing this bug as well.
no way for qe to confirm, closing