i have two NSManagedObject classes, UserInfo and Department (to-one and to-many relationship respectively )here is my code,
// UserInfo.h
#interface UserInfo : NSManagedObject
#property (nonatomic, retain) NSDate * bDate;
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#property (nonatomic, retain) NSString * userName;
#property (nonatomic, retain) Department *dept;
#end
//Department.h
#class UserInfo;
#interface Department : NSManagedObject
#property (nonatomic, retain) NSString * id;
#property (nonatomic, retain) NSString * post;
#property (nonatomic, retain) NSString * location;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSSet *user;
#end
#interface Department (CoreDataGeneratedAccessors)
- (void)addUserObject:(UserInfo *)value;
- (void)removeUserObject:(UserInfo *)value;
- (void)addUser:(NSSet *)values;
- (void)removeUser:(NSSet *)values;
#end
for searching particular user from db i used, which works well
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"firstName contains[cd] %#", NameTosearch];
but, now what kind of predicate i can use to fetch all users with info connected to particular department?
You can also use a comparison with an object in predicates.
I.e. If you have a pointer to a Department instance in dept:
...withFormat:#"dept = %#", dept"
internally CoreData will replace the %# with the managed object identifier of the object.
Search using
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"firstName contains[cd] == %#", NameTosearch];
Follow this blog for more info http://rajneesh071.blogspot.in/
And follow my answer StackOverflow
Related
I'm quite new to Objective-C. I've been trying to fetch from my core data entity. The following code fetches the right row because it only returns 1 result.
but when I try to NSlog() it to see its values what I get is:
iGym[3922:c07] (
"<User: 0x8397690> (entity: User; id: 0x8341580 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)"
)
I am used to PHP that I just do a var_dump() and i get all the information in the array... specially when I am expecting 16 results as this entity has 16 fields.
Could anyone tell me how could I inspect that array?
and also, most important, how do I ultimately do this: fetch the Gender field of the matched email field and assign it to a NSString variable.
The query i want to do in sql is SELECT Gender FROM myTable WHERE email = "something#something.com;
-(NSInteger*)selectedGenderMethod
{
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
request.predicate = [NSPredicate predicateWithFormat:#"email = %#",_currentUser];
NSError *error = nil;
NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];
NSString * someVar= [matches[0] objectForKey:#"email"];
NSLog(#"%#",someVar);
//More to come once this is sorted
return 0;
}
This fetching code is happening in my genderPickerViewController : UIViewController
NSLog(#"%#", matches[0]);
returns
<User: 0x83a6b00> (entity: User; id: 0x83995f0 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)
This is my User.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface User : NSManagedObject
#property (nonatomic, retain) NSDate * dob;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSNumber * firstTime;
#property (nonatomic, retain) NSString * gender;
#property (nonatomic, retain) NSString * height;
#property (nonatomic, retain) NSNumber * idFB;
#property (nonatomic, retain) NSNumber * idUserExternal;
#property (nonatomic, retain) NSNumber * idUserInternal;
#property (nonatomic, retain) NSNumber * isPT;
#property (nonatomic, retain) NSString * language;
#property (nonatomic, retain) NSString * metricSystem;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * nickname;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * surname;
#property (nonatomic, retain) NSString * weight;
#end
User.m:
#implementation User
#dynamic dob;
#dynamic email;
#dynamic firstTime;
#dynamic gender;
#dynamic height;
#dynamic idFB;
#dynamic idUserExternal;
#dynamic idUserInternal;
#dynamic isPT;
#dynamic language;
#dynamic metricSystem;
#dynamic name;
#dynamic nickname;
#dynamic password;
#dynamic surname;
#dynamic weight;
#end
Is the user object a NSDictionary or an NSArray by chance? If that is the case you are simply logging out the object, you will need to specify a specific entity in the object to output.
For example if it's a NSDictionary
NSString *name = [matches[0] objectForKey:#"name"];
NSLog("Name %#", name);
You could also try
NSString *email = (User *)matches[0].email;
NSLog("Email %#", email);
What you are doing is right, you usually get the full objects and not only one attribute, Core Data is an object storage.
So you only need [((User *)matches[0]) gender] to get the gender of the returned user.
If you want to get some extra information from objects you must implement:
- (NSString *)description
It is used for converting to string and logging.
You can use the following code,
[NSPredicate predicateWithFormat:#"email CONTAINS[c] %#", _currentUser];
OR
[NSPredicate predicateWithFormat:#"email CONTAINS[cd] %#", _currentUser];
then you can print the fetched results as type po OBJECT_NAME.PROPERTY in debug area
I am getting the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Illegal container for relationship: value = (
"<CVStar: 0x6a2e990> (entity: CVStar; id: 0x6a2e980 <x-coredata:///CVStar/t20E391D3-6FA6-4521-84FE-EAA9469E012029> ; data: {\n \"date_created\" = \"1312050431.973905\";\n message = nil;\n user = \"0x6a2be10 <x-coredata:///CVLogin/t20E391D3-6FA6-4521-84FE-EAA9469E012030>\";\n})",
"<CVStar: 0x6a2aca0> (entity: CVStar; id: 0x6a2b0d0 <x-coredata:///CVStar/t20E391D3-6FA6-4521-84FE-EAA9469E012031> ; data: {\n \"date_created\" = \"1312011314.591517\";\n message = nil;\n user = \"0x6a36490 <x-coredata://B8EAEF54-AC90-46F0-B442-93077B937C3F/CVLogin/p28>\";\n})"
); relationship = "stars".'
Any idea on how to debug this? I have a class CVMessage which has an NSArray of CVStar.. I've setup a relationship on the model from CVMessage as a one to many to CVStar (A CVMessage can have many CVStar). This however doesn't work, how is this even possible?
Here's some code:
#class CVLogin;
#class CVTopic;
#interface CVMessage : NSManagedObject {
}
#property (nonatomic, retain) NSNumber * date_created;
#property (nonatomic, retain) NSString * message;
#property (nonatomic, retain) NSNumber * mid;
#property (nonatomic, retain) CVLogin * creator;
#property (nonatomic, retain) NSArray * stars;
#property (nonatomic, retain) NSArray * embeds;
#property (nonatomic, retain) CVTopic * topic;
#property (nonatomic, assign) BOOL options;
#end
#interface CVStar : NSManagedObject {
}
#property (nonatomic, retain) NSNumber * date_created;
#property (nonatomic, retain) CVLogin * user;
#end
some screenshots if it helped
NSManagedObjects cannot have NSArray as a property type, since Core Data does not support it. Instead, use NSSet, which should describe a relationship. If you must have ordered data, then add a property which describes the order of your objects. Also, keep in mind that Core Data is much more than a persistent store, and is not a SQL database. You'll do much better if you don't think of things as rows and columns, but rather as objects.
I have the following class:
#interface Topics : NSObject {
NSNumber * _until_id;
NSArray * _topics;
}
#property (nonatomic, retain) NSNumber * until_id;
#property (nonatomic, retain) NSArray * topics;
#end
/////////////////////////////////////////////////////////////////////////
#class Login;
#interface Topic : NSObject {
NSString * _name;
Login * _creator;
NSNumber * _message_count;
NSNumber * _date_latest_message;
NSNumber * _date_created;
NSNumber * _tracked;
NSNumber * _unread;
NSNumber * _tid;
NSString * _kind;
NSNumber * _id;
}
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) Login * creator;
#property (nonatomic, retain) NSNumber * message_count;
#property (nonatomic, retain) NSNumber * date_latest_message;
#property (nonatomic, retain) NSNumber * date_created;
#property (nonatomic, retain) NSNumber * tracked;
#property (nonatomic, retain) NSString * kind;
#property (nonatomic, retain) NSNumber * unread;
#property (nonatomic, retain) NSNumber * tid;
#property (nonatomic, retain) NSNumber * gid;
#property (nonatomic, readonly) NSString* topicNavURL;
#end
Now I am trying to access a particular topics name using:
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TTTableTextItem class]];
[mapping mapKeyPath:#"topics.name" toAttribute:#"text"];
[mapping mapKeyPath:#"topics.topicNavURL" toAttribute:#"URL"];
However, this fails as it says that it doesn't find name as topics is a NSArray. Is there a way to do KVC if I am dealing with array? Is it just NSString's then?
Yes, you cannot access an array directly using KVC.
However you can use aggregates like #sum, #avg to calculate the sum, average
For ex: #sum.message_count will give you the total message count of all messages in the array. You don't have to write any loop for that.
I using the project named coredatabooks from apple developer examples.
#import
#interface Book : NSManagedObject
{
}
#property (nonatomic, retain) NSNumber *active;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *author;
#property (nonatomic, retain) NSDate *copyright;
#end
I need to use a bool object, but I don't know how to implement it in my class.. I'm using right now NSNumber, using an integer (0, 1) but is not a Bool... maybe it will be more correct to use this ...
#property (nonatomic, retain) bool active;
but is not working.
You can still use an NSNumber, but pack it from a bool:
[NSNumber numberWithBool: myBool];
and unpack it to a bool:
[myNSNumber boolValue];
I'm getting this same error, but I've checked to make sure the properties were set correctly in the .h file.
Here's the code:
NSUInteger theSizesCount = [theWho.theSizes count];
The error is "error: request for member theSizes in something not a strucutre or union. This .m file is importing 6 .h files, and 4 of them have the same properties in theWho, but it's related to various Super Classes. This .m file is implementing only one of them, and theWho and theSize are sythesized.
Also, in this code, and theSizes variable is green, but theWho variable is not. Plus, the error is happening in multiple places for NSUIntegers, NSMutableArray etc.
Where am I going wrong? Some of the header file code is below.
// TheSize.h
#class TheWho;
#interface TheSize : NSManagedObject
{
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *amount;
#property (nonatomic, retain) TheWho *theWho;
#property (nonatomic, retain) NSNumber *displayOrder;
#end
and..
//
// TheWho.h
//
#interface ImageToDataTransformer : NSValueTransformer {
}
#end
#interface TheWho : NSManagedObject {
}
#property (nonatomic, retain) NSString *instructions;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSSet *theSize;
#property (nonatomic, retain) UIImage *thumbnailImage;
#property (nonatomic, retain) NSManagedObject *image;
#property (nonatomic, retain) NSManagedObject *type;
#end
#interface TheWho (CoreDataGeneratedAccessors)
- (void)addTheSizesObject:(NSManagedObject *)value;
- (void)removeTheSizesObject:(NSManagedObject *)value;
- (void)addTheSizes:(NSSet *)value;
- (void)removeTheSizes:(NSSet *)value;
#end
I checked my declarations again, and I had to add an "s" to the NSSet *theSize entry. Those errors are gone. Thanks all for the help.
You still need to define the member variables. The #property directive declares the accessor methods, not the underlying members.
#interface TheWho : NSManagedObject {
NSString *instructions;
NSString *name;
NSSet *theSize;
UIImage *thumbnailImage;
NSManagedObject *image;
NSManagedObject *type;
}
You're trying to access theWho.theSizes but according to your header file, you should be trying theWho.theSize (without the trailing s).
Unless, there's a typo in how you've typed it here, and it wasn't copied directly.