Using C pointers with automatic reference counting (ARC)
Video: Using C pointers with automatic reference counting (ARC)At the Worldwide Developers Conference in 2011, Apple introduced an extension to Objective-C called Automatic Reference Counting, or ARC. Like many object-oriented languages, Objective-C uses the concept of object ownership to keep track of how many places an object is used so that it may know when to retain and release memory associated with objects. This was traditionally handled manually by the programmer using retain and release method calls on the globally-inherited NSObject class. It was messy, but it worked.
Viewers: in countries Watching now:
The iOS software development kit (SDK) includes the popular SQLite library, a lightweight yet powerful relational database engine that is easily embedded into an application. In this course, Bill Weinman teaches you how to build an RSS reader for iOS devices, integrating XML data and a streamlined interface. He explains how to use the SQLite database, display information in a table view, code view controllers, and create a preferences pane for your app. The resulting application is optimized for all iPhone and iPad displays.
- Prototyping the app
- Coding and working with a testbed
- Creating an Objective-C interface for SQLite
- Designing a database schema
- Creating the view controllers
- Reading and writing to the database
- Parsing the RSS feed with NSXMLParser
- Updating the item view with feed items
- Implementing the pull to refresh gesture for iOS 6
- Creating a universal application with multiple views
Using C pointers with automatic reference counting (ARC)
At the Worldwide Developers Conference in 2011, Apple introduced an extension to Objective-C called Automatic Reference Counting, or ARC. Like many object-oriented languages, Objective-C uses the concept of object ownership to keep track of how many places an object is used so that it may know when to retain and release memory associated with objects. This was traditionally handled manually by the programmer using retain and release method calls on the globally-inherited NSObject class. It was messy, but it worked.
The new Automatic Reference Counting feature, or ARC, automates this process. ARC is a pre-compilation phase that automatically creates all of your retain and release calls, so you no longer have to do that manually. This is a fantastic addition to Objective-C, and it makes your job as a programmer a lot easier. Unfortunately, it also creates some new problems, one of which is how to interface with libraries written in plain old C. Let's take a look at an example. Here we have BWDB.h, which is a wrapper, around a C library for the SQLite 3 data base.
And originally I had written this wrapper before ARC was introduced, and it worked just fine, and when ARC was introduced, it gave me trouble around this bit of code right here, which is the Fast Enumeration interface. And the reason for this is that we're passing it plain old C pointers like this stack buffer and ARC was having trouble dealing with that. Now, SQLite is of course, widely used for persistent storage throughout iOS and Fast Enumeration is a feature that's not actually very commonly used but is very, very handy.
SQLite is of course, written in plain old C, and it has no concept of retain and release. It just uses plain old malloc and free for its memory allocation. For the most part, this is not an issue, and it's completely transparent, but when you need the interface with the foundation protocol like Fast Enumeration, it becomes an issue. So, if we look over here in the .h file, we see this NSDictionary enumRows, and this is used right there, and right here in the Fast Enumeration countByEnumeratingWithState method.
So, what I had to do is in order to make this work is I had to declare this as unsafe, un-retained storage. And this is just one of the four storage classes that are available. It's actually a new one that was introduced with ARC and basically it just tells the compiler, when you're doing your ARC stuff, ignore this completely. And that works fine because it's statically allocated, and it doesn't need to be retained and released anyway. The other thing that I had to do is I had to qualify this id for stackbuf with unsafe_unretained.
And again, this was not in the example for the Fast Enumeration on how to implement it in your own class as it's not very well-documented. It's only documented by example and so I kind of had to figure this out myself. But in a nutshell unsafe_unretained tells the system to bypass reference counting and not to zero out pointers. And this makes this particular feature work with ARC. Now, using Fast Enumeration is a convenience, it's not really a requirement. I find it very convenient and powerful and useful.
It's also powerful and useful to understand how ARC works, not just to use it but to understand what it does and how to use it especially in exceptional use cases like this one. So, if you'd like to know more about ARC, this page on the llvm website explains all the gruesome details about how Automatic Reference Counting is implemented in Objective-C. It's a bit dense, but I find that it was worth the effort to read it.
Find answers to the most frequently asked questions about iOS SDK and SQLite: Building Data-Driven Apps (2013) .
Here are the FAQs that matched your search "" :
- Q: Why is the RSSDB library in the exercise files different than the one in the videos?
A: The RSSDB library had to be updated to work around a bug in the iOS 7 SDK.
There is a bug in the iOS 7 SDK that prevents the BWDB fast enumeration implementation from working on a device. The symptom is code that runs fine on the emulator, but not on a device. iOS devices use an ARM processor, while the emulator runs on your Mac's Intel processor. This points to the LLVM ARM code generator as the source of the bug. Because the bug appears to be in the LLVM compiler, it may be some time before it is fixed.
As a workaround we have changed the getFeedIDs and getItemIDs methods in the RSSDB library so they don't use Objective C fast enumeration.
Please note that this same bug also affects some of the BWDB testbed code in Chapter 2. The result is that it will run on the emulator but not on a device.
- Q: After upgrading to Xcode 5.1 I get an error that says:
"Used type va_list (aka_builtin_va_list) where arithmetic or pointer type is required"
- A: Please download the exercise files again to get the latest version of the BWDB library.
- Q: I'm using Xcode 6. Why am I getting error messages with the exercise files?
- A: A lot has changed in iOS since this course was released. The author is in the process of rewriting the code and updating the course for iOS 8. In the meantime he has prepared a version of the app that works in iOS 8 and Xcode 6. Download it here:http://ios.bw.org/
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.