Why is "didChangeValueForKey:withSetMutation:usingObjects:" overriding discouraged - ios

The apple documentation for NSManagedObject discourages overriding of "didChangeValueForKey:withSetMutation:usingObjects:".
Why?
As long as I call [super ..] this seems to work. Is there a reason not to? Or is there a more efficient way of capturing changes to it's own relationships?

'Seems' to work... Doesn't mean it will always work. Apple 'strongly discouraging' things generally means that they aren't telling us something about how the code works behind the scenes and you could easily trip yourself up.
To manage changes you should usually use dependent keys or implement custom accessor methods.

Related

Prevent Method Swizzling Objective-c

I am trying to search to prevent the Method Swizzle in my current library but more or less every documentation or blog post that i found are about how to implement Swizzling.
There are couple of question that i have regarding Method swizzling that i could not find all across.
How to detect the Method Swizzling at runtime?
In case there is a Method Swizzle how to prevent it ?
I know the dangers of method swizzling and have already gone through the related posts here but could not find the related information to prevent it.
If there is any documentation or blog post that available on the above topics, I would really appreciate the assistance.
Avoiding method swizzling is simple; use functions rather than methods (i.e. do things the old-fashioned way without objects, in C++, or at least in Core Foundation). But if you're using the ObjC runtime, you can be swizzled.
You can in principle detect swizzling by caching all your IMP pointers, for example using class_getMethodImplementation in something like +load or maybe a C++ constructor on a global variable (which get run before main()), and then re-checking all your IMP pointers at various times to make sure they haven't changed.
That probably wouldn't be too hard, but it's difficult to imagine what all of this would achieve. If someone has your framework binary, it wouldn't be a major effort to to patch it to remove your check. Somewhere in the source code, there's got to be a if (swizzled) { ... }, and that's going to translate into a branch-if conditional instruction in the assembly. You stick a debugger on the system, wait for the branch to "ah! we're swizzled" to occur, note the point where it happens, and patch that byte to be "branch-if-not" or just add an unconditional jump.
Slowing that attack down, even a little, requires substantial obfuscation. There are techniques (that mostly don't work that well), but they only work by being kept secret. That's the difference between obfuscation and security.
Short answer is you really can't achieve this in any stable way. That means you either need a team devoted to constantly coming up with new, more advanced obfuscations and updating them regularly as new attacks emerge (i.e. how a company like Blizzard or Apple prevent hacking), or you'll need to find a way not to need this.
Simplest answer? Work mostly in C++ and use ObjC classes as little as possible (which will prevent swizzling, but not reverse engineering or patching). Or accept that swizzling is not avoidable.
(BTW, if there were even a "I'm willing to do whatever it takes" answer to this question, then Apple would just use that technique to make jailbreaking impossible. The fact that iPhones get jailbroken regularly suggests the difficulty of the problem.)

AppDelegate property or Singleton Object?

This is more of a design best practice question:
When you are designing the structure of lets say a location based app. The location Manager is obviously an important instance and should be given easy access for other objects.
Should you have it as a property of appDelegate? or a singleton on its own?
Under what scenario would you prefer one over the other?
I understand both would work, but I want to make sure I'm doing things the right way, not just hacking everything together.
Your inputs are much appreciated!
Neither.
Pass in a location manger object via a custom init method or property where ever you need it.
This will conform with the SOLID principals S, O & D (single responsible, open-close, dependency inversion).
Also testing with mocks will be possible more easily.
The worst choice IMO is to store things in the app delegate. See What describes the Application Delegate best? How does it fit into the whole concept? for much more on that. In short, the app delegate is the Application Delegate. It is not "the Application Dumping Ground for Globalish Things."
Singletons are a long-established approach in ObjC, via the shared... pattern. After decades of popularity, and extensive use within the core Cocoa frameworks (NSUserDefaults, NSNotificationCenter, NSApplication, NSFontManager, NSWorkspace, UIDevice, etc. etc. etc.), they have in recent years fallen into some disregard in favor of other techniques, particularly "dependency injection," which is just to say "assigning to a property."
After years of using singletons in ObjC, I am coming around to the DI way of thinking. The improvements in testability and the improved clarity of dependencies are quite nice. That said, sometimes DI can lead to awkward transitions, particularly when dealing with storyboards.
So, I would say:
When practical, just construct objects and assign them to properties on the objets that need them.
If you have a lot to pass around, consider collecting them into a single "configuration" object and pass that around (but this hurts modularity somewhat).
If DI creates chaos (particularly if it leads to a lot of passing objects to things just so they can pass the object on to something else), or if it forces a lot of storyboard segue code that you could otherwise avoid, singletons are a well-established and well-respected pattern in Cocoa and are not wrong. They are a Cocoa Core Competency.
If you ever find yourself calling (MyAppDelegate *)[[UIApplication sharedApplication] delegate], then you're doing something wrong.
Make another singleton for location management. Single responsibility is a first principle of SOLID.
Think about do you really need it in AppDelegate ?
For Location Manager for example, no you don't. You better keep the location manager with all its related methods in a separate class to keep the singularity principle as #vladimir said, and to be able to reuse it later.
AppDelegate is responsible for handling what happens when the app launch and/or going to the background, initializing core data, registering to push notification and other 3rd party libraries like parse,...
Adding other things to appDelegate will make it grow larger by time and it would be very hard to maintain.
When to add things that don't belong to AppDelegate to it? I think when the app is small, you know it won't scale up, and you are required to favor time over clean code.
Check the AppDelegate responsibilities , and this answer by Matt
You can create multiple instances of CLLocationManager and use them wherever you need them.
If you create one instance and try to share it then you'll have trouble trying to forward the delegate methods around or try to re-implement them as notifications leading to a big mess.

Is iOS tutorial "Your Third iOS App: iCloud" out of date?

I'm following along through the three tutorials provided by Apple for iOS newbies. The first two seemed to very closely follow guidelines that, from what I gather, fit the newest enhancements and recommended coding habits for Objective-C, but the third one doesn't seem to.
Not sure if a link will work: https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloud101
It explicitly tells you to call #synthesize for all the declared properties. Isn't this unnecessary as long as you don't care to customize the backing variable's name?
It tells you to make forward declarations of methods. Is this still required by the compiler? When are forward declarations unnecessary?
It frequently makes use of instance variables that are accessed directly instead of using properties. Aren't properties the preferred method for data storage? When would it be preferable to avoid using a property? I'm guessing they were doing this as a way to create private variables, maybe. There are no anonymous categories used in this tutorial so maybe those became available after the tutorial was written.
I think there are more. Just want to be sure I'm not missing out on some potential techniques for the tool belt here in my attempt to avoid what may be bad habits in modern Objective-C. The fine print at the end shows that it was updated in April 2013, but maybe they didn't bother to update all the code style.
They probably just didn't update the code style. Also, Apple sample code is often quite diverse in its style. Regarding specific questions:
This is now generally unnecessary - but in some cases is, for instance, when a class conforms to a property declared in a protocol.
Within the same file, methods that are implemented but not declared elsewhere are visible. Generally, declaration of methods in a header is only required when the methods must be called from outside the class.
Using instance variables can be a matter of taste I suppose. In dealloc and initializers it's often recommended to use ivars directly in case of side effects in the property implementations.
You don't need to manually #synthesize properties anymore
Forward declarations in the implementation file are not needed anymore.
Some people like ivars, some people like properties, just make sure you use them consistently.

A tool in Xcode to check wether a function retains or not?

I'm currently working on an iOs application, and there is this one thing that is such a pain in the... well, a pain anyway : I always have to check the documentation to know wether an object property is retained or not (for instance, the setDelegate of the UITextField assigns the delegate and doesn't retain it, whereas the setFont function retains... https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html)
It's... a pain. Is there a way to know such a thing directly in Xcode ?
Thanks in advance
Delegates are a special case because what you are setting as a delegate is usually an object which would have a lifetime exceeding or equal to the object it is delegating for (i.e, a view controller would be the delegate of a text field). Because of this design pattern delegates are assigned, not retained, to avoid retain cycles. If you are creating a new object to act as the delegate for some other object, then you would have to retain it, but it doesn't quite smell right to be doing it that way.
For the rest of the cases, I really don't understand what your issue is, or why you are checking the documentation. You don't need to care about the retaining or otherwise that framework objects do to their properties. You only need to care about the retains and releases that you have made in your own code.
Do you have an example of a non-delegate property in a UIKit object that you have to retain yourself because the UIKit object is not retaining it?
I'm new to Objective C. But I think you don't really need to know. You just pass the property and it's up to the class (UITextField in this case) to retain it or not. You don't have to keep it around after passing it unless you need it for something else.
Also, try switching to ARC, a lot less of headaches for beginners.
Easy answer: switch to supporting iOS5 only and use ARC. You (mostly) don't need to worry about this kind of thing.
But, really, you don't need such a tool anyway. The conventions are very simple.
If you alloc, retain or copy something, you need to release it at some point
Otherwise you don't
Delegates are, in practice, no different. Why? Well, the delegate has to be around at least as long as the object with the delegate. So, unlike your setFont: example, you are very unlikely to do alloc; delegate = ...; release.

iOS but why delegate

I see everywhere in iOS programming that delegate is used....I am not sure what for is being used.
Can you please explain me about it?
When you need objects to behave differently from each other, you can either give them different implementations (often by subclassing, as is done with UIViewController), or by delegation. The difference is that polymorphism in the first case is achieved by making the objects of different type, whereas in the second, polymorphism is achieved by making objects of the same type delegate certain bits of functionality to objects of arbitrary type.
The reason people like delegation so much is that it allows a much cleaner class hierarchy: you don't need to subclass every little thing, just to respond to a few hooks. That's why we use delegation.
If you want to know the mechanics of delegation, and how to do it in your own classes, you're welcome to see my article Using custom delegates in Objective-C.
It's all just about optional function calls.
Objects can designed to optionally call methods in other objects. Some of those optional methods can be grouped together into a delegation pattern.
A delegate is simply another object that wants to be called by those methods. When you set a delegate, you let the calling object know that somebody wants to be called (accept the option), and who to call (the "delegate") whenever the option comes around.
If nobody wants to be the delegate, then the calling object with the option will usually just do some default behavior. So this pattern ("delegation") is a good way to be able to modify defaults, but not have to do so, and without requiring creating a lot of subclasses (which can get more confusing more quickly in the opinion of some people).
Here's another Stack Overflow answer about Delegates, I gave awhile back.
See Apple's Documentation on these design patterns.

Resources