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.
Related
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
I am creating an iOS app that deals with real estate listings. The current structure is as follows:
Device contacts server and downloads an index file which contains all current record IDs on the server and the last time each record was modified.
Each core data record on the device is checked against the index file and either:
a) the record is up to date and nothing happens.
b) the record is out of date, is deleted from the device and reloaded from the server.
c) the record is not part of the index file and is deleted from the device.
d) no record is found on the device with the listing ID and is downloaded from the server.
I use an index file because I have to download only a small part of the data for each record in order to compare.
My problem is this, I currently have about 1250 test records on the server. With the current setup it is taking almost 3 minutes (using WiFi) to complete the initial index check routines. There has to be a better way to handle a large number of records in a iOS app. Am I wrong for trying to load all the records in core data up front?
For reference I am including the ListingRecord.h
#property (nonatomic, retain) NSString * amenitiesText;
#property (nonatomic, retain) NSString * bodyLabel1;
#property (nonatomic, retain) NSString * bodyLabel2;
#property (nonatomic, retain) NSString * bodyLabel3;
#property (nonatomic, retain) NSString * brokerID;
#property (nonatomic, retain) NSString * companyID;
#property (nonatomic, retain) NSDate * dateCreated;
#property (nonatomic, retain) NSString * descriptionText;
#property (nonatomic, retain) NSString * displayPrice;
#property (nonatomic, retain) NSString * featuredListing;
#property (nonatomic, retain) NSString * headerLabel;
#property (nonatomic, retain) NSData * headerPhoto;
#property (nonatomic, retain) NSString * lastUpdate;
#property (nonatomic, retain) NSNumber * latitudeData;
#property (nonatomic, retain) NSNumber * listingID;
#property (nonatomic, retain) NSString * listingType1;
#property (nonatomic, retain) NSString * listingType2;
#property (nonatomic, retain) NSString * listingType3;
#property (nonatomic, retain) NSString * listingType4;
#property (nonatomic, retain) NSString * listingType5;
#property (nonatomic, retain) NSString * listingType6;
#property (nonatomic, retain) NSString * listingType7;
#property (nonatomic, retain) NSString * listingType8;
#property (nonatomic, retain) NSNumber * longitudeData;
#property (nonatomic, retain) NSNumber * numberPrice;
#property (nonatomic, retain) NSData * photo1;
#property (nonatomic, retain) NSData * photo2;
#property (nonatomic, retain) NSData * photo3;
#property (nonatomic, retain) NSData * photo4;
#property (nonatomic, retain) NSData * photo5;
#property (nonatomic, retain) NSString * pinLabel;
#property (nonatomic, retain) NSData * thumbnailPic;
#property (nonatomic, retain) NSString * sessionID;
The index file contains the listingID and the lastUpdate and compares those against the core data records on the device.
It seems like there are two bottle necks, the communication with the server and the check routine. To help solve the first bottle neck I highly recommend JSON, if used correctly it should reduce the size of the data to be passed from the server to the device and its very very easy to work with. It should also help with the second bottle neck, the check routine as you simply grab the returned data from the server, convert it to a JSON object representation with one line of code and then you are basically dealing with a NSDictionary of values.
If you need to do all this at once, I would arrange your flow logic so that you can do this "initial index check routines" in the background thread.
Another option would be sending the index file to the server and let your backend server do the work and it returns only the lists for adding, deleting, updating, etc. This would depend on if your backend server is capable of doing this.
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 want to read the value from my DatePicker and store it in my singleton, but I get an exception when trying to do this.
Here is the singleton interface:
#interface Helper : NSObject {
NSMutableArray *array;
//Information For Filter page
NSMutableArray *towns;
NSMutableArray *types;
//Shared resources to apply filters
NSDate *toDate;
NSDate *fromDate;
NSString *selectedType;
NSString *selectedTown;
//Shared resource of which button was clicked
int clicked;
int clickedCell;
int clickedContext;
}
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic, retain) NSMutableArray *towns;
#property (nonatomic, retain) NSMutableArray *types;
#property (nonatomic) int clicked;
#property (nonatomic) int clickedCell;
#property (nonatomic) int clickedContext;
#property (nonatomic, retain) NSDate *toDate;
#property (nonatomic, retain) NSDate *fromDate;
#property (nonatomic, retain) NSString *selectedType;
#property (nonatomic, retain) NSString *selectedTown;
+(id)sharedManager;
#end
This is the function where the exception happens
-(void) viewWillDisappear:(BOOL)animated
{
Helper *myHelper = [Helper sharedManager];
if(myHelper.clickedContext == 0)
{
if(myHelper.clickedCell == 0)
{
//causes exception
myHelper.fromDate = [self.fromDatePicker date];
}
else
{
//causes exception
myHelper.toDate = [self.toDatePicker date];
}
}
else
{
if(myHelper.clickedCell == 0)
{
myHelper.selectedTown = [myHelper.towns objectAtIndex:[self.townPicker selectedRowInComponent:0]];
}
else
{
myHelper.selectedType = [myHelper.types objectAtIndex:[self.typePicker selectedRowInComponent:0]];
}
}
[super viewWillDisappear:animated];
}
declaration of pickers
#property (nonatomic, retain) IBOutlet UIDatePicker *toDatePicker;
#property (nonatomic, retain) IBOutlet UIDatePicker *fromDatePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *typePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *townPicker;
#synthesize part
#synthesize typePicker, toDatePicker, fromDatePicker, townPicker, townsView, toDateView, typesView, fromDateView;
Any idea's why?
Thanks
If your outlet is not assigned in interface builder this can happen.
If you think it is, try running the app with NSZombieEnabled = YES and see if you get any error messages.
I found the problem. In my Helper Class I should have assigned not retained. So it should have been
#property (nonatomic, assign) NSDate *toDate;
#property (nonatomic, assign) NSDate *fromDate;
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];