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.
Related
I currently have the following Core Data Model:
Category
#interface Category : NSManagedObject
#property (nonatomic, retain) NSSet *subcategory;
Subcategory
#interface SubCategory : NSManagedObject
#property (nonatomic, retain) Category *category;
#property (nonatomic, retain) NSSet *items;
Items
#interface Item : NSManagedObject
#property (nonatomic, retain) SubCategory *subCategory;
I want to get all items in a category, however when I write:
NSLog(#"Items in Category:%#", self.category.subcategory.items)
I get nothing.
What would be the way to fetch the information? Since I am receiving the class with a property I added in the ViewController:
#property (strong, nonatomic) Category *category;
One possible solution is to use the Key-Value Coding
Collection Operator
#distinctUnionOfSets:
NSSet *allItems = [self.category valueForKeyPath:#"subcategory.#distinctUnionOfSets.items"];
(Note that a better name for the to-many relationship "subcategory" would be
"subcategories".)
Alternatively, you can execute a fetch request on the "Item" entity with a predicate
using the inverse relationships:
[NSPredicate predicateWithFormat:#"subCategory.category == %#", self.category];
The advantage of this method is that the intermediate "SubCategory" objects are
not loaded into memory.
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".
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];
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.
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.