0

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

amitsbajaj
  • 1,304
  • 1
  • 24
  • 59
  • This short video lecture should probably help you get around. http://www.youtube.com/watch?v=Mjdxjgdbf-k – Javier Quevedo Jul 24 '13 at 22:21
  • Thanks Javier. I have taken this course online, as well as the year after and while it's really helpful to understand the concepts of CoreData, I am very happy with CoreData but am just not sure how to efficiently map out the model for this particular application. – amitsbajaj Jul 25 '13 at 06:16

1 Answers1

0

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!

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Dear Muni, thank you so much for your reply and that is immensely helpful. It gives me a whole new way of seeing a solution to the problem, so I am extremely grateful. I have a couple of questions that I need to follow up with - my apologies for the newbie questions! Would the relationship between the Transaction and Location.. would there be an attribute called location, etc in the Transaction which links to the Location? Also, in the fetchrequest, I would use the Transaction entity to bring in the results, with the relationships back to the Person, Occasion, etc? Sorry, and thank you! – amitsbajaj Jul 25 '13 at 11:25
  • sorry.. Mundi - slight typo there! :) – amitsbajaj Jul 25 '13 at 12:37
  • You are basically right. Relationships have names (like attributes). You set them in the model editor. Make sure the to-many relationships have plural names (such as "transactions") - this makes it easier to read. Yes, you can fetch the transaction and filter with NSPredicate, sort with NSSortDescriptor, using the relationships as key paths. These APIs are not so difficult, and once you master them they are tremendously powerful. – Mundi Jul 25 '13 at 14:45
  • That's so helpful Mundi - thank you so much. This has really given me a great platform to start and move forward with this project. You are a great help and seeing it from this perspective, I have a new found understanding and appreciation for how it can work. Thanks again Mundi - massively appreciated. – amitsbajaj Jul 25 '13 at 16:00