Trying to set Core Data Relationships causing crash - objective-c

I have a few entities, one named "List" and one named "Task". The both have multiple properties and only 1 relationship. The list property has a to-many relationship called hasTasks, whose destination is task.
This is the Task.h file that is generated for me.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Task;
#interface List : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * number;
#property (nonatomic, retain) NSNumber * tasks;
#property (nonatomic, retain) NSNumber * totalTime;
#property (nonatomic, retain) NSSet *hasTasks;
#end
#interface List (CoreDataGeneratedAccessors)
- (void)addHasTasksObject:(Task *)value;
- (void)removeHasTasksObject:(Task *)value;
- (void)addHasTasks:(NSSet *)values;
- (void)removeHasTasks:(NSSet *)values;
#end
Whenever I run a line of code such as :
[self.list addHasTasksObject:task];
it crashes my app without any error message. Any help would be appreciated.

I had this same problem and I fixed it in the .xcdatamodeld file. I had accidentally selected the ordered arrangement checkbox for a relationship without re-generating the NSManagedObject classes. Unchecking this box and re-building solved this problem for me. If you are still having a problem, try re-generating your NSManagedObject classes.

Related

Why declare the variables of public properties on the interface with the property declarations?

I'm looking at this code below and trying to figure out which of the two of us... (me, or the person who wrote it) doesn't know what they're doing.
Why did he/she declare properties, then declare matching variables as well?
I can't ask the person because this project is inherited from a group long gone.
#interface LayerList : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *tvList;
ArcGisViewController *mapController;
NSArray *fileList;
}
#property (nonatomic, strong) IBOutlet UITableView *tvList;
#property (nonatomic, strong) ArcGisViewController *mapController;
#property (nonatomic, strong) NSArray *fileList;
-(void)visibleSwitchValueChanged:(id)sender;
-(IBAction) Cancel;
#end
This was the usual way in a day when you had to synthesize the properties manually before auto-synthesizing was introduced.

How can I cast my NSURLSessionDownloadTask to my custom NSURLSessionDownloadTask (inheritance)?

I have created a custom NSURLSessionDownloadTask named VJSessionTask and I have just added some custom things like a type (enum) and a custom object (id):
#interface VJSessionTask : NSURLSessionDownloadTask
typedef enum types
{
LS, LSH, DL, UL, RM, TH
} type;
#property enum types type;
#property (strong, nonatomic) id customObject;
#property (strong, nonatomic) NSString *progressNotif;
#property (strong, nonatomic) NSString *doneNotif;
#property (strong, nonatomic) NSURL *tmpFile;
#end
And when I do this:
VJSessionTask *taskSession = (VJSessionTask *)[self.prioritySession downloadTaskWithRequest:listFileRequest];
// init taskSession with its type
taskSession.type = LS;
I get this error:
-[__NSCFLocalDownloadTask setType:]: unrecognized selector sent to instance 0x1556198f0
Then I come to you as I don't understand or I don't know how to do that...
Thank you in advance ;)
NSURLSessionTasks are not strictly speaking subclass-able unfortunately. This is evident in that the system can queue a data task and return a NSCFLocalDownloadTask (presumably meaning that the task will return its content from the cache).
The best way to go about doing this is to borrow on from the architectural decision of AFNetworking and have individual taskDelegates that monitor all the responses an individual task works on. Then when you want to find the data relating to a task you can query your dictionary of taskDelegates. Each task has a unique identifier that you can use to key your dictionary with.
In AFNetworking you can see the taskDelegate is defined as follows:
#interface AFURLSessionManagerTaskDelegate : NSObject <NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
#property (nonatomic, weak) AFURLSessionManager *manager;
#property (nonatomic, strong) NSMutableData *mutableData;
#property (nonatomic, strong) NSProgress *progress;
#property (nonatomic, copy) NSURL *downloadFileURL;
#property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
#property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler;
#end
#implementation AFURLSessionManagerTaskDelegate
and subsequently retrieved as follows:
- (AFURLSessionManagerTaskDelegate *)delegateForTask:(NSURLSessionTask *)task {
NSParameterAssert(task);
AFURLSessionManagerTaskDelegate *delegate = nil;
[self.lock lock];
delegate = self.mutableTaskDelegatesKeyedByTaskIdentifier[#(task.taskIdentifier)];
[self.lock unlock];
return delegate;
}
See this post for more info

One to Many Core Data binding issues

I have 2 Core Data objects. Items and TimeLog. The items object has a one to many relation with TimeLog and I am using the IB and Array Controller to automatically populate the 2 NSTableView's
The top table view is for the items. When you select an item the bottom table should populate with the time logs.
However when I add an item, the application crashes with an error
<_NSFaultingMutableSet 0x102e0e790> addObserver:forKeyPath:options:context:] is not supported. Key path: date
I am using an Array Controller to populate all the information automatically. When I create and add and item I am not setting anything for the timeLog relationship because there is no time to add when they first add the item. The object is saving as I have logging that is triggered after the core data save event.
Items.h
#class TimeLog;
#interface Items : NSManagedObject
#property (nonatomic, retain) NSString * itemId;
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * itemType;
#property (nonatomic, retain) NSSet *timeLog;
#end
#interface Items (CoreDataGeneratedAccessors)
- (void)addTimeLogObject:(TimeLog *)value;
- (void)removeTimeLogObject:(TimeLog *)value;
- (void)addTimeLog:(NSSet *)values;
- (void)removeTimeLog:(NSSet *)values;
#end
TimeLog.h
#class Items;
#interface TimeLog : NSManagedObject
#property (nonatomic, retain) NSString * time;
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) Items *item;
#end
What is causing this error and how do I get rid of it?
I resolved this by creating another NSArrayController for my TimeLog and setting up the table like so.
TimeLog Arra Controller
Set Controller Content -> Content Set -> Bind to Items array controller. Model Key path to timeLog
Then each column of the table.

object property in objective-c

#interface Category : NSObject
#property(nonatomic) NSInteger ID;
#property(nonatomic, retain) NSString *Name;
#property(nonatomic, retain) NSString *Description;
#end
product class
#interface Product : NSObject
#property(nonatomic) NSInteger ID;
#property(nonatomic, retain) NSString *Name;
#property(nonatomic, retain) NSString *Description;
#property(nonatomic, retain) Category *category;
#end
How do I create a property of type Category. Is it possible at all?
You do it just like you have, except you need to add a forward declaration of your Category class in Product.h (before your #interface line):
#Class Category;
And then import the header in Product.m
#import "Category.h"
1) If you class Product is defined in separate file, then:
#import "Category.h" // file, where class Category is defined
#interface Product : NSObject
#property(nonatomic) NSInteger ID;
#property(nonatomic, retain) NSString *Name;
#property(nonatomic, retain) NSString *Description;
#property(nonatomic, retain) Category *category;
#end
2) If your classes Product and Category are defined in same file then you don't need any additional import but be sure that class Categiry is defineed earlier then Product.
3) In the case when two classes have objects of type of each other then your should use #class tag:
#class Product;
#interface Category : NSObject
#property(nonatomic, retain) Product *product;
#end
#interface Product : NSObject
#property(nonatomic, retain) Category *category;
#end
Yes, it is possible, and the syntax that you have posted is correct. If you are having a problem, perhaps it may be because you need to do
#import "Category.h"
at the top of Product.h, and you will need to make sure to #synthesize category. But since you have been using other properties, I assume you knew that already.

Generated Custom Managed Object Class does not build

I have built an data model in Xcode and I have generated a custom managed object class from one of the entities in the model. I am not sure why but I don't think the custom class is building properly.
For example if I just type some random stuff in the middle of a class declaration like:
#class MyOtherClass;
#interface MyClass : NSManagedObject
{ !!!!!!!THIS SHOULD CAUSE A SYNTAX ERROR!!!!!!!!!!!!
}
#property (nonatomic, retain) NSDate * myDate;
#property (nonatomic, retain) NSNumber * myNumber;
#property (nonatomic, retain) NSSet* otherEntities;
#end
it builds successfully. I have no idea why.
Any thoughts?
Are you sure you're including the .m and .h files in your project?