1

I'm just getting started with Core data, (and I'm also trying to use Magical Record). I'm creating a pretty simple Payment tracking app.

I would like to save a Payment object that has an array of Debtors. This is what my Payment object looks like

@class Debtor;

@interface Payment : NSObject
@property (strong, nonatomic) NSString *paymentAmountString;
@property (strong, nonatomic) NSString *titleString;
@property (strong, nonatomic) NSArray *debtorsArray;
@property (strong, nonatomic) NSDate *dueDate;
@property (strong, nonatomic) NSString *notesString;
@end

And the debtorsArray is an array of Debtor objects

@interface Debtor : NSObject
@property (strong, nonatomic) NSString *nameString;
@property (strong, nonatomic) NSString *amountOwedString;

How should I go about saving this object since it contains an array. Do I need to create two different Entities, with a relationship between Payment and Debtor? How exactly do I do this, and how would I ensure that they are fetched properly?

3
  • What exactly does debtorsArray contain ? Can they be serialised some how. You could then use transformable type. Commented Aug 16, 2015 at 18:37
  • it is an array of Debtor objects, I updated my post with that Debtor Object Commented Aug 16, 2015 at 18:38
  • Please see my answer below and please do accept if it works for you. Thanks. Commented Aug 16, 2015 at 19:29

1 Answer 1

1

Create only one entity for Payment. You will have to use the 'Transformable' data type for your attribute debtorsArray within this entity.

Then implement the following methods in your Debtor class:

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.nameString forKey:@"nameString"];
    [aCoder encodeObject:self.amountOwnedString forKey:@"amountOwnedString"];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super init]){
        self.nameString = [aDecoder decodeObjectForKey:@"nameString"];
        self.amountOwnedString = [aDecoder decodeObjectForKey:@"amountOwnedString"];
    }
    return self;
}

Entity should be fetched normally like any other fetch query.

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a reason I would use this instead of a to-many relationship?
That actually depends on your data size I think. I tend to avoid creating multiple nested entities if my data is considerable in size. This way I could be possibly saving a few extra entity calls and fetch requests. I find this way simpler.
I'll give this a shot, I personally can't confirm it works though yet, I still need to figure out how to save and fetch using Magical Record.
@Andy Sure! I can confirm that this works as I've used it for exactly the same situation in my project:) but I'll wait for your confirmation. Just asking, what do you exactly mean by a magical record?
Magical Record, makes core data simplified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.