How do I iterate through NSSet<NSManagedObject *> in objective C? - ios

This is my class... I am populating it fine when loading the XML via web service, however when I'm passing the object to another controller I can't get the list of purchase order items into an array I can put into a table.
#interface PORecord (CoreDataProperties)
+ (NSFetchRequest<PORecord *> *)fetchRequest;
#property (nonatomic) int64_t poActivityId;
#property (nullable, nonatomic, retain) NSSet<NSManagedObject *> *receivedRefReasons;
#property (nullable, nonatomic, retain) NSSet<NSManagedObject *> *purchaseOrderItems;
#end

Related

Sorting the contacts list alphabetically and show in tableview groups?

I have array of contacts(NSObject)
#interface Contact : NSObject
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *number;
#end
contacts = #[contact1, contact2, contact3, contact3, contact4, contact5];
i want to show them alphabetically under sections in UITableview
dynamically.
any suggestions would be more appreciated?

CoreData: How to properly set created entity attributes

I have a Core Data Entity, let's call it "Record", and I have several attributes that I need to set with data from other objects/entities (as well as setting up relationships).
Let's say "Record" has the following attributes:
#interface Record (CoreDataProperties)
+ (NSFetchRequest<Record *> *)fetchRequest;
#property (nullable, nonatomic, copy) NSNumber *superRecordID;
#property (nullable, nonatomic, copy) NSNumber *otherDataID;
#property (nullable, nonatomic, copy) NSNumber *active;
#property (nullable, nonatomic, copy) NSDate *createDate;
#property (nullable, nonatomic, copy) NSDate *updateDate;
#property (nullable, nonatomic, copy) NSString *uuid;
#property (nullable, nonatomic, retain) SuperRecord *superRecord;
#end
I know for things such as active, I can set the default to 1 from XCode and for things such as createDate, updateDate and uuid, I can override the awakeFromInsert method.
My question is: what is the best practice for setting the other values and relationships upon creation? The only way I can think of that I know should work is to create the instance THEN set all the attributes, but is there a better way I can do this? Where I pass in the additional values/objects are parameters and set the attributes/relationships on creation?
Simply write a convenience allocator like this:
- (instancetype)initWithSuperRecord:(SuperRecord*)superRecord active:(BOOL)active … context:(NSManagdObjectContext*)context
{
self = [super initWithEntity:… /* in subclasses you typically know that */
insertIntoManagedObjectContext:context];
if( self )
{
// Set the properties you get from the args
}
return self;
}
+ (instancetype)newWithSuperRecord:(SuperRecord*)superRecord active:(BOOL)active … context:(NSManagdObjectContext*)context
{
return [[self alloc] initWithSuperRecord:superRecord … context:context];
}
Some suggestions: The superID seems to be a property of the super record. Don't store it twice. Moreover you should check, whether you need an ID at all. This is not OOPish. Boolean values should by typed to boolean values. Use YES and NO or #YES and #NO instead of 1 and 0.
Typed in Safari.

Estimated data and server data does not match Parse.com

I'm developing a chat app and I'm using Parse for backend.
I have a Discussion table to save Discussion between 2 user.
#interface Discussion : PFObject <PFSubclassing>
#property (nonatomic, retain) PFUser * customer;
#property (nonatomic, retain) PFUser * creator;
#property (nonatomic, retain) Quote * quote;
#property (nonatomic, retain) NSDate * lastMessageTime;
#property long messageCount;
#end
I have a Message table to save message.
#interface Message : PFObject <PFSubclassing>
#property (nonatomic, retain) PFUser * sender;
#property (nonatomic, retain) Discussion * discussion;
#property (nonatomic, retain) NSString * content;
#property (nonatomic, retain) PFFile * image;
#property (nonatomic, retain) PFFile * imageThumb;
#property (nonatomic, retain) PFFile * video;
#property (nonatomic, retain) PFFile * videoThumb;
#property (nonatomic, retain) PFGeoPoint * location;
#property (nonatomic, retain) PFFile * sound;
#end
When user chatting, I use cloud code to update lastMessageTime and messageCount.
I want to show a list of people with small label that shows messageCount, and I have a NSTimer to call it automatically each 3.0 seconds.
But when I get new Dicussion, its estimatedData and serverData do not matchs.
I have no idea about this. Please give me your advice.
I have same problem when working on Android Parse SDK.
I think you have edited messageCount on both server side via Cloud Code script and client side via Object-C.
To fix it, you must edit only on client side or server side and sync to another side.
Thanks.

Access private interface property by public method

I would like to ask you some opinion about what I'm doing. I know it works, but I'm not sure is proper to do what I'm doing.
I have a superclass Building that need to expose two NSString, name and description. No one should be able to modify those variables apart their subclasses.
To get this result I have created two public method on the base class:
#interface Building : NSObject
- (NSString *)name;
- (NSString *)description;
#end
Then on each subclass I'm creating a private interface with name and description properties and let the magic happen.
#interface Barrack()
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *description;
#end
#implementation Barrack
#synthesize name, description;
...
#end
What you guys think about this?Is this a proper way to get this kind of result, anyone have better ideas about this topic?
Thanks for your opinion.
Best,
Enrico
Declare readonly properties in the interface, readwrite in the implementation class extension. No need for #synthesize. This is one of the main reason class extensions were added to Objective-C.
in Building.h
#interface Building : NSObject
#property (nonatomic, strong, readonly) NSString *name;
#property (nonatomic, strong, readonly) NSString *description;
#end
In Barrack.m
#interface Barrack ()
#property (nonatomic, strong, readwrite) NSString *name;
#property (nonatomic, strong, readwrite) NSString *description;
#end
#implementation Barrack
...
#end

RestKit CoreData one to many relationship mapping, the To Many part not working

I am using RestKit 0.20 to map 2 entities.There is a one to many relationship.
Teacher<->>SchoolClass
Here is the Teacher.h
#class SchoolClass;
#interface Teacher : NSManagedObject
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#property (nonatomic, retain) NSNumber * teacherId;
#property (nonatomic, retain) NSSet *teachesClass;
#end
#interface Teacher (CoreDataGeneratedAccessors)
- (void)addTeachesClassObject:(SchoolClass *)value;
- (void)removeTeachesClassObject:(SchoolClass *)value;
- (void)addTeachesClass:(NSSet *)values;
- (void)removeTeachesClass:(NSSet *)values;
#end
Here is the SchoolClass.h
#interface SchoolClass : NSManagedObject
#property (nonatomic, retain) NSString * classCodeId;
#property (nonatomic, retain) NSString * classDesc;
#property (nonatomic, retain) NSString * classRoom;
#property (nonatomic, retain) Teacher *classTeacher;
#end
The code for the relationship mapping is:
[classMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"teacher" toKeyPath:#"classTeacher" withMapping:teacherMapping]];
The results are that in the SchoolClass objects, the classTeacher properties are correctly added. However in the Teacher objects, the teachesClass properties are all empty. Is this expected behavior or I missed something?
Thanks
Ray
Somehow the problem is gone now. All the relationships now working fine. Not sure exactly what happened. Perhaps because I did a reset for the simulator after made a json change. Previously the json results had a problem that caused both side of the relationship problems. After it was fixed, the SchoolClass objects were fine but Teacher objects had relation problems. Now both are fine.

Resources