Why is my ivar not getting set on NSManagedObject Subclass - ios

I have a project using CoreData. I use Mogenerator to generate the subclasses.
When I set the value of a property, this value isn't actually assigned. Each subsequent time I try to set the value, the previous value I set it to was not assigned.
This worked fine as my underlying data framework was Mantle, but since moving to CoreData, this stopped working. I rely on KVO to keep some UIView objects up-to-date with the model.
Again, the ivars of a CoreData NSManagedObject subclass do not seem to take the values I assign them.
Consider the following interface:
#interface Light : _Light{}
/**
Light / Color Properties
*/
#property (nonatomic, assign) CGFloat brightness; // 0...1
#property (nonatomic, assign) CGFloat hue; // 0...1
#property (nonatomic, assign) CGFloat saturation; // 0...1
#property (nonatomic, assign, getter = isEnabled) BOOL enabled;
#property (nonatomic, readonly) UIColor *color; // derived from the above
- (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation; // it often makes sense to set these together to generate fewer KVO on the color property.
#end
and the following .m file:
#interface Light ()
{
CGFloat _hue, _saturation, _brightness;
UIColor *_color;
}
#property (nonatomic, assign) BOOL suppressColorKVO;
#property (nonatomic, readwrite) UIColor *color;
#end
#implementation Light
#synthesize suppressColorKVO = _suppressColorKVO;
- (void)setHue:(CGFloat)hue saturation:(CGFloat)saturation
{
BOOL dirty = NO;
if (saturation != _saturation) {
// clamp its value
[self willChangeValueForKey:#"saturation"];
_saturation = MIN(MAX(saturation, 0.0f), 1.0f);
[self didChangeValueForKey:#"saturation"];
dirty = YES;
}
if (hue != _hue) {
[self willChangeValueForKey:#"hue"];
_hue = MIN(MAX(hue, 0.0f), 1.0f);
[self didChangeValueForKey:#"hue"];
dirty = YES;
}
if (dirty) {
if (!_suppressColorKVO) {
[self setColor: self.color];
}
}
}
// other stuff... the color accessors are also custom. Derived from the h, s, b values.
#end
I assume I'm not playing nice with CoreData, but I have no idea what's wrong. These hue, saturation, brightness are all 'transient' (not in the core data sense) because they are constantly updated by some hardware we are interfacing with so there's no need to save their state.

If hue and saturation are properties in your model then you should be setting their values using setPrimitiveValue:forKey: (or the associated generated primitive methods).
That said, your code all looks custom as model attributes would be NSNumber instances and mogenerator would create value methods for you. So I'm going to guess that these attributes you have aren't backed in the model and that's why they aren't being stored.
So, add the attributes to the model and access the values using the appropriate methods.

In the end it had nothing to do with CoreData. This approach above DOES work with CoreData objects. You can have in a subclass some "transient" properties that exist outside of the CoreData NSManagedObject and you can create your own ivars for them, and your own accessors, and it cooperates.
In relation to this question, I have a complex system that also sends some commands to some hardware, and the hardware returns a response whether it accepted the command with the current status. It turns out I had a bug in that handler which was setting these values back to some unexpected value.
What helped me debug it were using watchpoints in the debugger. Really handy feature! You set a watchpoint and it will break in the debugger whenever the memory address of your variable is set with a new value. (A tiny bit more on that here)

Related

Can I use category and runtime to add a BOOL type property "isScrolling" to UIScrollView?

I used to add a NSString *type property to UIButton,today however,I want to add a BOOL type property isScrolling to UIScrollView to indicate whether the scrollView is scrolling in the same way but there showed something wrong,here is my code:
#import <UIKit/UIKit.h>
#interface UIScrollView (Util)
#property (nonatomic,assign) BOOL isScrolling;
#end
#import <objc/objc-runtime.h>
#interface UIScrollView ()<UIScrollViewDelegate>
#end
#implementation UIScrollView (Util)
static void *strKey = &strKey;
- (void)setIsScrolling:(BOOL)isScrolling{
objc_setAssociatedObject(self, & strKey, isScrolling, OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)isScrolling{
return objc_getAssociatedObject(self, &strKey);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.isScrolling = YES;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
self.isScrolling = NO;
}
#end
And the error is:
Is there any way to deal with thses errors and can we use category and runtime to achieve the goal of adding a BOOL property to UIScrollView to indicate whether the scrollView is scrolling?
Hope someone can give me some advice,thanks a lot.
An associated object must be just that, an object, and so a value of the non-object BOOL type won't work unless wrapped as an object. Fortunately that is pretty easy:
In the call to objc_setAssociatedObject change isScrolling to #(isScrolling) and change OBJC_ASSOCIATION_ASSIGN to OBJC_ASSOCIATION_RETAIN_NONATOMIC. This will create and pass an NSNumber object, the second change requesting that this object's lifetime be tied to that of the first parameter, self.
In isScrolling change objc_getAssociatedObject(self, &strKey) to [objc_getAssociatedObject(self, &strKey) boolValue]. This will extract the BOOL value from the stored NSNumber object.
HTH
Try this :
Try to convert boolean to nsnumber.
-(void)setIsScrolling:(BOOL)isScrolling{
objc_setAssociatedObject(self, & strkey), [NSNumber numberWithBool:isScrolling], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
As the error is showing implicit conversion of BOOL to id, you need to send a object instead of primitive types.
The method signature for objc_setAssociatedObject is
/**
* Sets an associated value for a given object using a given key and association policy.
*
* #param object The source object for the association.
* #param key The key for the association.
* #param value The value to associate with the key key for object. Pass nil to clear an existing association.
* #param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*
* #see objc_setAssociatedObject
* #see objc_removeAssociatedObjects
*/
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);
Above you can see the value should be object.
Change your code
#property (assign, nonatomic) BOOL isScrolling; to #property (strong, nonatomic) NSNumber *scrolling;
And change OBJC_ASSOCIATION_ASSIGN to OBJC_ASSOCIATION_RETAIN_NONATOMIC in your case objc_setAssociatedObject(self, &strkey, scrolling, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
And use [_scrolling boolValue] for checking.
You can't set the primitive data type as an AssociatedObject, It's an Object. Convert bool to NSNumber when you are saving the data. Convert NSNumber to bool while reading the data.
OBJC_ASSOCIATION_ASSIGN - Specifies a weak reference to the associated object.
OBJC_ASSOCIATION_RETAIN_NONATOMIC - Specifies a strong reference to the associated object, and that the association is not made atomically.
.h File :
#import <UIKit/UIKit.h>
#interface UIScrollView (ScrollViewCategory)
#property (nonatomic, strong)NSNumber *isScrolling;
#end
.m File
#interface UIScrollView ()
#end
#import <objc/runtime.h>
#implementation UIScrollView (ScrollViewCategory)
#dynamic isScrolling;
- (void)setAssociatedObject:(id)object {
objc_setAssociatedObject(self, #selector(associatedObject), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)associatedObject {
return objc_getAssociatedObject(self, #selector(associatedObject));
}

Defining variables, setters and getters in Objective-C

I have a hard time getting myself introduced to Objective-C and iOS programming. I tried to search for this problem, but the results weren't explained simple enough.
So I have a viewController.h and a viewController.m. In the .h I add my Labels/Buttons etc. between #interface and #end
#property ( nonatomic, strong ) UILabel *facebookLoginTextLabel;
#property ( nonatomic, strong ) UIButton *facebookLoginButton;
And in the .m I'm simply synthesizing them between #implementation and the first method
#synthesize facebookLoginTextLabel = _facebookLoginTextLabel;
#synthesize facebookLoginButton = _facebookLoginButton;
The problem I'm facing now, is what to do with other values? As an example. I have a boolean value. Where do I declare it? How do I set it? How do I get the value? It's way too confusing for me. The same counts for an NSInteger?
I want, dependent on the ifcase, set the NSInteger to 0, 1 or 2. How do I achieve this? I tried getting and setting like this
.h
#property ( nonatomic ) CGFloat *width;
.m
#synthesize width = _width;
viewDidLoadMethod
_width = [self returnScreenWidth];
returnScreenWith method
- ( CGFloat ) returnScreenWidth {
return [UIScreen mainScreen].bounds.size.width;
}
This doesn't work. Why? How do I set, how do I get? How do I declare variables? Way too confusing from a PHP and an Android-Developer.
First of all, it is CGFloat width not CGFloat *width as it is a C struct, not an Objective C class where you define the property in the header file:
#property (nonatomic) CGFloat width;
Instead of accessing the under-score instance variable, just use self.width whereby you access the #property using self.
Remember, all basic/primitive data types like (int, float, char, BOOL) need not be used as you are doing, always use assign.
Yes BOOL is a primitive, as this is typedef to char.
It should be defined as :
#property (assign) <primitivetype> propertyName;
Also to note down, with new compiler (I hope you are using Xcode4.2 onwards i.e LLVM),
#synthesize propertyName is done automatically by the compiler as
#synthesize propertyName = _propertyName;
atomic and nonatomic depends on your requirement, so is the pointer to int or float.

Add #propertys to Core Data class

I made few classes via Core Data. And I need some additional #propertys for one of that classes in runtime. This #propertys are responsible for download progress and I don't want to store them in Core Data DB. I tried to use a separate extension class:
#interface MyClass ()
{
CGFloat _downloadProgress;
NSInteger _downloadErrorCounter;
BOOL _downloadAllStuff;
BOOL _downloadUserCanceled;
}
#property (nonatomic, assign) CGFloat downloadProgress;
#property (nonatomic, assign) NSInteger downloadErrorCounter;
#property (nonatomic, assign) BOOL downloadAllStuff;
#property (nonatomic, assign) BOOL downloadUserCanceled;
#end
But private variables are not visible out of MyClass, and #propertys compile all right, but in runtime i get -[MyClass setDownloadErrorCounter:]: unrecognized selector sent to instance.
Can anyone suggest me some solution?
The easiest solution (if you don't want to modify the Xcode generated class files) is to add the properties to the Core Data model and define the
properties as transient. Transient properties are not saved to the store file.
Another option is to use a tool like "mogenerator", which generates two class files for each
entity, one for the Core Data properties (which is overwritten if the the model changes),
and one for your custom properties (which is not overwritten).
Update: Starting with Xcode 7, Xcode creates both a class and
a category for each managed object subclass, compare NSManagedObject subclass property in category. Custom properties can be added to the class
definition which is not overwritten when the model changes.
Just add
#synthesize downloadErrorCounter = _downloadErrorCounter;
...
in #implementation. Note, not #dynamic.
When trying to use the #synthesize solution i got the error:
#synthesize not allowed in a category's implementation.
Solution was to use associated objects as described in this blog: http://kaspermunck.github.io/2012/11/adding-properties-to-objective-c-categories/
MyManagedObject+Additions.h
#property (strong, nonatomic) NSString *test;
MyManagedObject+Additions.m
NSString const *key = #"my.very.unique.key";
- (void)setTest:(NSString *)test
{
objc_setAssociatedObject(self, &key, test, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)test
{
return objc_getAssociatedObject(self, &key);
}

iOS5 Core Data and NSNumber is there autoboxing/unboxing?

Coming from Java, I'm pretty used to autoboxing, where an int is automatically wrapped to an Integer when needed, and an Integer may be unboxed into a primitive. Is there something similar that I can rely upon in iOS5?
currently, I'm using core data, and it takes a lot of typing to keep having to type
number.intValue
//or
number.boolValue
is there some way to use an NSNumber directly in equations and such? for example:
int x = 5+ nsNumberInstance;
Furthermore, every time I need to re-assign a number in the core data, I'm creating a new object like this.
managedObject.dynamicProperty = [NSNumber numberWithInt: int];
is there a better way to change the value of an already created NSNumber? What kinds of nifty shortcuts may I use to save myself from carpal tunnel 10 years from now?
Thank you!
Actually, when you are in your data model, and you use the "Create NSManagedObject subclass" menu item, there is an option that you can select titled "Use scalar properties for primitive data types".
This automatically handles this for you in many cases.
Here are some examples:
BOOL:
#property (nonatomic, retain) NSNumber * aBool;
becomes
#property (nonatomic) BOOL aBool;
NSDate:
#property (nonatomic, retain) NSDate * aDate;
becomes
#property (nonatomic) NSTimeInterval aDate;
Integer 32:
#property (nonatomic, retain) NSNumber * aNumber;
becomes
#property (nonatomic) int32_t aNumber;
and
Float:
#property (nonatomic, retain) NSNumber * aFloat;
becomes
#property (nonatomic) float aFloat;
NSDecimalNumber and NSString stay the same.
You can change these yourself in the previously generated header file if you have already generated the subclasses and the accessor methods will automatically update without having to re-generate the subclass.
Outside of Cocoa bindings, I can't think of many other places that have autoboxing of scalar types in Cocoa or Cocoa touch, so unfortunately you're out of luck there.
You don't really gain anything by working with NSNumbers in calculations, so dealing with the objects there isn't really necessary. It's much easier to work with scalar types, and then convert back and forth between NSNumbers when storing these numbers in Core Data, arrays, etc. The one case where you'd want to stay in this form would be NSDecimalNumbers, which do not represent numbers as your standard floating point values, and thus avoid the glitches you see when trying to work with decimals in those types.
Core Data stores objects, so you're not going to get around that at a base level, but you can make your life a little easier by using custom accessors on your NSManagedObject subclasses that take and return scalar values. Apple has an example of this in the "Managed Object Accessor Methods" section of the Core Data Programming Guide, where they show how to set up an accessor for a CGFloat value, instead of using an NSNumber:
#interface Circle : NSManagedObject
{
CGFloat radius;
}
#property CGFloat radius;
#end
#implementation Circle
- (CGFloat)radius
{
[self willAccessValueForKey:#"radius"];
float f = radius;
[self didAccessValueForKey:#"radius"];
return f;
}
- (void)setRadius:(CGFloat)newRadius
{
[self willChangeValueForKey:#"radius"];
radius = newRadius;
[self didChangeValueForKey:#"radius"];
}
#end
As a side note, using the dot syntax for -intValue and -boolValue, while it works, is not recommended. These are not properties, but one-way methods that extract values from the NSNumbers. Use brackets when dealing with them to make this clear in your code.

Core Data: implementing property that is dependent on other

I have following situation in my project (in which I use Core Data): I have an entity which has two BOOL properties: isCompleted and isNonVisit. It also has third property: NSNumber *status - the value of the property depends on both isCompleted and isNonVisit values.
When either of the BOOL property changes, I want status property to be actualised automatically.
All three properties must be present in underlying database, since I use fetchedResultsController that makes use of status property (as sort descriptor and as sectionNameKeyPath).
I came up with following solution:
in .h file:
#property (nonatomic, retain) NSNumber *isCompleted;
#property (nonatomic, retain) NSNumber *isNonVisit;
#property (nonatomic, retain) NSNumber *status;
- (NSNumber *)calculateStatus; //Returns proper status value based on isCompleted and nonVisit property values.
in .m file:
#dynamic isCompleted;
#dynamic isNonVisit;
#dynamic status;
- (void)setIsCompleted:(NSNumber *)newValue
{
[self willChangeValueForKey:#"isCompleted"];
[self setPrimitiveValue:newValue forKey:#"isCompleted"];
[self didChangeValueForKey:#"isCompleted"];
self.status = [self calculateStatus];
}
- (void)setIsNonVisit:(NSNumber *)newValue
{
[self willChangeValueForKey:#"isNonVisit"];
[self setPrimitiveValue:newValue forKey:#"isNonVisit"];
[self didChangeValueForKey:#"isNonVisit"];
self.status = [self calculateStatus];
}
The solution seems to work.
So, my question is: Is it OK? Am I violating some rules of CoreData or KVO?
Thanks for any suggestions.
Your method seems sound.
The only suggestion I would have is to reduce the redundancy by extracting the boolean information from the status with accessor methods rather than storing them. You still should be able to use the desired predicates for your fetch requests just using the status variable. But the overhead of storing this extra information should be minimal.

Resources