Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-7523

iOS: Use blocks and remove "return caches" from code

    Details

      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.

        Attachments

          Activity

            People

            • Assignee:
              blainhamon Blain Hamon
              Reporter:
              stephentramer Stephen Tramer
            • Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-11992
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Integration