Question

I have my first app in the pipeline and I know exactly how it's going to work; the part that is racking my brain is the Core Data Model in the background.

The premise of the app is record when you give money to someone and when you receive money from that person, or receive/give a gift. It's not going to be very complicated because it's targeted for the older generation.

The UI principles are going to be a tabbed-based approach with the first tab being the "timeline". A reference to who you have given to and received from in a timeline.

The data displayed in the timeline is: - date at the top of each "section" lets call it - name of event - name of person - location - currency - amount

I figure I'd display this in a cell with the title, subtitle, and perhaps merge the occasion and location. If the user clicks on a cell, it will show all entries and transactions between that person and you.

The user will also be able to search by name of event, name of person, location and/or date.

With this in mind, I have it in my head how to map out the Core Data Model but it's just not making sense.

Because I want to use an NSFetchedResultsController, I understand that I cannot fetchrequest across multiple entities, but I could use a relationship. This is where I get lost.

One idea I have is to have a single entity with nameOfEvent, nameOfPerson, Date, Currency and Amount as the attributes. That way, in the Timeline view, I could bring in all of the information for each cell.

This at the same time doesn't make full sense to me because there should be more entities. For example, I would imagine:

Occasion (Entity) Name (Attribute) Location (Attribute) Date (Attribute)

Person (Entity) Name (Attribute)

Gift (Entity) MoneyAmount (Attribute) Currency (Attribute) Gift (Attribute - optional)

Action (Entity) Given (Attribute - Bool) Received (Attribute - Bool)

That is my other idea, but it seems that fetching each cell will use a lot of effort, if that's even possible. There will never be more than 100 + cells per user of the app, so we're not talking about images, etc.

I'm stuck with this approach; do I go ahead and use relationships? If so, what?

I am lost and any help would be appreciated!

Thanks, Amit

Was it helpful?

Solution

You are on the right track!

Do not worry about the complexity or data volume - Core Data can handle 100.000s of records easily on a mobile device. With relationships you can fetch and filter this data very quickly and efficiently.

Your data model could look like this:

Person <---->> Transaction 
Transaction <<----------> Location
Transaction <<----------> Occasion
Transaction <-----------> Gift

Person (name, birthday, etc)
Transaction (createdDate, status [given, pending, canceled, etc.], category, type)
Location (name, lat, lon, address...)
Occasion (title, category)
Gift (type, amount, currency, shortName, notes)

When this is set up correctly, you can sort and group easily using a NSFetchedResultsController and you can access all the properties easily with the convenient dot notation without having to worry much about fetches, e.g.

NSUInteger numberOfGifts = person.transactions.count;

NSSet *nearLocations = [locations filteredSetUsingPredicate:locationPredicate];
NSSet *nearTransactions = [nearLocations valueForKeyPath:@"transaction"];

etc.

Enjoy!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top