Aleksandar • Vacić

iOS bits and pieces

Masterclass: Core Data tidbits

I have been using Core Data for a while now, several years at least. Over time, I have learned how it works internally and learned to side-step possible issues and shortcomings. The following is a unordered list of tidbits.

Masterclass: KVC magic tricks

KVO is very, very dangerous. No matter how skillful you believe to be, strive to avoid it as much as you can.

If you really need KVO, then use Facebook’s KVOController. Don’t ever attempt to write the required code on your own. Just look at the source code of this Facebook’s bag of voodoo if you need reasons.

Don’t ever use KVO to monitor changes in Core Data objects.

Masterclass - Notifications

One way to declare and assign the notification name is using global strings in .h file:

extern NSString *const XXAPIManagerDidAcquireSessionNotification;

then assigning the values in the .m file:

NSString *const XXAPIManagerDidAcquireSessionNotification = @"XXAPIManagerDidAcquireSessionNotification";

However, long strings like this are hard to scan when reviewing the code. Thus I prefer to namespace them using structs.

Masterclass: Objective-C mandatory rules

(puttin’ the grumpy hat on, full Crusty mode)

Don’t use instance variables

Before properties, Objective-C code was usually like this:

@interface XXSomething {
NSString *_internalVar;

and then followed by explicit getter and setter methods in implementation that read or set this _internalVar. If you are still using this – stop. When I see code written like this, I dismiss it as obsolete and move on to something else.

Use @property. Always and ever.