CoreData fetch all related objects - core-data

I have two related entities in core data
a book entity
#property (nonatomic, retain) NSString * author;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSSet *reviews;
a review entity
#property (nonatomic, retain) NSString * reviewer;
#property (nonatomic, retain) NSString * reviewText;
#property (nonatomic, retain) Book *book;
the last property in each is the relationship.
I have a book object
Book *book
How do I fetch from CoreData all review object that are related to 'book'?

book.reviews should return a set of review objects for that book.

Related

Problems accessing 1:N relationship in entity

https://github.com/lokming/QuestionBank
I have entities: Bank,Section,Subsection,Set,Question.
I am having problems accessing relationship "NSSet Subsection" in entity Section and getting the message "[UITableViewCell thesubsection]: unrecognized selector sent to instance" from this code
in CRSectionVC.m which fills a tableview cell
- (NSArray *)allQuestions
{
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision" ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
}
I can however access the "NNSet section" relationship in Bank entity using this code
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
_section2 is declared in CRSubsectionVC.h
#property (strong, nonatomic) Section *section2;
The storyboard is
1. CRMasterViewController which displays ‘category’ attribute from Bank entity into a tableview,
Bank.h
#class Section;
#interface Bank : NSManagedObject
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSSet *thesection;
#end
Bank.m
#implementation Bank
#dynamic category;
#dynamic thesection;
#end
When I tap a ‘category’ I segue and pass a Bank object to CRDetailViewController. I use following code:
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
to fetch section relationship (NSSet *thesection) ‘division’ attribute from Bank into tableview.
Section.h
#class Bank, Subsection;
#interface Section : NSManagedObject
#property (nonatomic, retain) NSString * division;
#property (nonatomic, retain) Bank *bank;
#property (nonatomic, retain) NSSet *thesubsection;
#end
Section.m
#implementation Section
#dynamic division;
#dynamic bank;
#dynamic thesubsection;
#end
If I tap a ‘section’ ’ I segue and pass a Section object to CRSubsectionVC named _section2. When I try to access NSSet *thesubsection to get ‘subdivision’ attribute using code
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision"ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
I get the error [UITableViewCell thesubsection]: unrecognized selector sent to instance. I cannot figure out why automated accessor ‘thesection’ works OK but not ‘thesubsection’ .
Subsection.h
#class Section, Set;
#interface Subsection : NSManagedObject
#property (nonatomic, retain) NSString * subdivision;
#property (nonatomic, retain) Section *section2;
#property (nonatomic, retain) NSSet *set;
#end
Subsection.m
#implementation Subsection
#dynamic subdivision;
#dynamic section2;
#dynamic set;
#end
Fixed the problem. A case of not copying code exactly from working template and understanding core data and table views.

NSManagedObject Custom Class --NSInvalidArgumentException unrecognized selector --

I have a custom NSManagedObject Class That Looks Like This.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Ingredient, MenuCategory, Price, ItemSize;
#interface Item : NSManagedObject
#property (nonatomic, retain) NSNumber * collected;
#property (nonatomic, retain) NSString * desc;
#property (nonatomic, retain) NSString * instructions;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * quantity;
#property (nonatomic, retain) NSNumber * selected;
#property (nonatomic, retain) MenuCategory *menuCategory;
#property (nonatomic, retain) NSSet *prices;
#property (nonatomic, retain) NSSet *itemSizes;
#property (nonatomic, retain) NSSet *itemIngredients;
-(NSMutableSet *)mutablePrices;
-(NSMutableSet *)mutableItemIngredients;
#end
#import "Item.h"
#import "Ingredient.h"
#import "MenuCategory.h"
#import "Price.h"
#import "ItemSize.h"
#implementation Item
#dynamic collected;
#dynamic desc;
#dynamic instructions;
#dynamic name;
#dynamic quantity;
#dynamic selected;
#dynamic itemIngredients;
#dynamic menuCategory;
#dynamic prices;
#dynamic itemSizes;
-(NSMutableSet *)mutablePrices{
return [self mutableSetValueForKey:#"prices"];
}
-(NSMutableSet *)mutableItemIngredients{
return [self mutableSetValueForKey:#"itemIngredients"];
}
#end
Nothing so special RIGHT ???
The Following Should Work Right ????
[item.mutablePrices addObject:newPrice]
BUT IT DOES NOT GIVES ME THE FOLLOWING ERROR
2014-03-30 10:25:34.594 restos[1192:60b] -[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
2014-03-30 10:25:34.597 restos[1192:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0'
BUT WHEN I DO THE FOLLOWING
[[item mutableSetValueForKey:#"prices"] addObject:newPrice];
WORKS JUST FINE --- I KNOW IS SOMETHING SIMPLE FOR SOME REASON I CAN NOT SEE IT ----
THANK YOU IN ADVANCE
The error message
-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
indicates that your item object is not an instance of the Item class. A possible reason could be that you did not set the class of the entity to "Item" in the Core Data
model inspector.
Generally it is less error prone if you let Xcode generate the NSManagedObject subclass files (from the Edit menu), and add your custom methods in a Class Category. Alternatively, use "mogenerator".

CoreData error failed

In my application i have used following code with core data,
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Adduser : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSString * contact_no;
#property (nonatomic, retain) NSString * address;
#property (nonatomic, retain) NSString * response;
#end
Method
-(IBAction)signup:(id)sender
{
Registration *adduser=[Registration new];//<-CoreData: error: Failed to call designated initializer on NSManagedObject class 'Registration'
adduser.name=txt1.text;
adduser.password=txt2.text;
adduser.email=txt3.text;
adduser.contact_no=txt5.text;
adduser.address=txtvu.text;
}
When i try to implement is shows the
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Registration'
with SIGABRT error?what wrong with my code?Please help
To create an instance of a managed object class you need to:
Registration *adduser = [NSEntityDescription insertNewObjectForEntityForName:#"Registration" inManagedObjectContext:context];

How to make predicate to Entity's customized property

This question is an ongoing one for my previous question.
I want to make a predicate to filter the entity by my customized property, this is the entity class
#interface Expense : NSManagedObject
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSNumber * amount;
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSString * note;
#property (nonatomic, retain) NSString * paidby;
//customized property
#property (readonly) int year;
#property (readonly) int month;
#property (readonly) NSString *yyyy_mmm;
#property (readonly) NSString *mm_yyyy;
#end
And this is my predicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"yyyy_mmm == %#", header];
When run:
error message NSInvalidArgumentException', reason: 'keypath yyyy_mmm
not found in entity
This is not completely unexpected, I searched and find out it has something to do with key-value, listener stuff. But I do not know what to do exactly.
Any help will be so appreciated, and BTW the coding block has some problem, please forgive me.
Make any property you need to use in the predicate an actual key on the entity in your data model. If you can set the value directly, great, do that. If not you either want to implement willSave or use KVO and update your predicate key values from the other values in your entity.

Messages app - Conversation lastMessage

This has to be a common question and I found many related here in SO, but none with the solution for this.
I have these 2 coredata models:
#class Message;
#interface Conversation : NSManagedObject
#property (nonatomic, retain) NSString * number;
#property (nonatomic, retain) NSSet *messages;
#property (nonatomic, retain) Message *lastMessage;
And
#class Conversation;
#interface Message : NSManagedObject
#property (nonatomic, retain) NSString * account;
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSString * message;
#property (nonatomic, retain) NSNumber * sentFlag;
#property (nonatomic, retain) NSString * status;
#property (nonatomic, retain) Conversation *conversation;
In my ConversationListViewController I fetch all conversations ordered by lastMessage.date.
Then the ConversationViewController receives a Conversationand lists its messages.
Right now I'm handling lastMessage manually, so every time I add a new message, in ConversationViewController, I also update conversation.lastMessage.
Is there a better way to do it?
Because right now, there's only one place I insert/update a new message, but if there was something like insert/update triggers that'd sound better..
I know I could fetch all Conversations and then sort them with a sortdescriptor using messages.#max.date but that doesn't sound like a good solution for a large pool of messages/conversations.
I really don't think that there is something more elegant or any kind of better way, then setting newMessage.conversation.lastMessage = newMessage.
Core Data posts notifications, every time it adds or changes an entity, but i don't think,that it would be easier to observe them, then adding this line of code every time you create a new message.

Resources