I’ve been making more use of threads and recently needed to allow two threads (neither of them the main thread) to coordinate with each other. In this particular case I was logging into Parse in one thread and moving some data from the Address Book to Core Data in the other thread. Once both threads completed, I wanted to call a routine that would allow me to communicate with Parse and update some records in the cloud as needed.
As an aside, I wanted to put in a quick plug for Parse. It’s a great service that allows me to persist objects in the cloud, perform queries, etc. The documentation is great and the team has been very responsive on their forums. I hope to write about my experience with Parse more extensively in the future.
Conceptually, it isn’t difficult to think about how to synchronize the completion of the threads before kicking off my update method. I just need to keep a flag somewhere that both threads can check.
@property (nonatomic) BOOL readyForParseUpdate;
I discovered a great debugging tool today from the article Flags: very useful when debugging with Instruments The article talks about how to put up flags from your code that appear in the Instruments window. This is a feature that is new to Instruments 4.0 and is described in the Apple docs at New Features in Instruments 4.0 Let’s jump to the end and show you an example of what you can put up in your Instruments display:
Note the yellow flags above the trace. These were generated in my code within a loop. If you click on a flag you can see the text that I generated for the flag “Loop index 38” along with the source file, line number of the code generating the flag and the method being executed.
In creating my first iPhone app which uses the Address Book, I discovered the world of memory management with iOS. I wanted to share a few things I’ve learned and provide lots of links for your reference.
ARC, retain counts and CFRelease
Admittedly, if you stick strictly to Foundation code, you probably don’t need to be concerned too much about what happens under the hood. With the introduction of ARC (Automatic Reference Counting), iOS takes care of much of this for you. It’s when you also need to deal with Core Foundation (CF), which the Address Book uses, that things become tricky. CF has a concept of object ownership and memory management using retain counts. An object can have multiple owners (retain count > 1), but when you are done with that object the retain count had better be zero or you’ll create memory leaks. To accomplish this, you need to use CFRelease().
Posted in iOS
Tagged address book, apple, arc, core foundation, foundation, iOS, leak, memory, objective c, retain count, zombie
After a year-long fascination with my first iPhone, I decided that I wanted to learn how to develop iPhone apps myself.
I was fortunate enough to have stumbled across a free online course from Stanford called Coding Together: Apps for iPhone and iPad also known as CS193P. The course was offered by Paul Hegarty starting in the summer of 2012 as a repeat of the course he offered in the Fall of 2011 titled iPad and iPhone Application Development. There was a new twist to the 2012 course though in that online students could now participate in a forum called Coding Together at Piazza.com. The Stanford homepage for the course supplements the free material available from iTunes with downloads for various demo projects discussed in lecture.
I began the course well after it started so was not able to participate actively at Piazza, but I still found the forum to be a rich source of information when I occasionally got hung up on a concept.
I did complete the lectures and homework assignments and am now working on the final project. A goal of the final project, per the instructor’s advice, is to achieve as much breadth as possible. Toward that end I am working on an enhanced Contacts app that is exposing me to the Address Book (Core Foundation based), Core Data, Table Views, and various other things covered in the course.
Some final thoughts for anyone thinking about taking this course:
- The instructor is fantastic!
- Keep your online notebook handy as you watch the lectures (I use Evernote).
- Leave yourself some good stretches of time for this course as it is pretty intense if you are new to Objective C.
- Do all the extra credit assignments! The homework tasks were very well thought out and the extra credit contributed a lot more toward my understanding of the material.