Attn: Core Data ninjas - need design advice

Discussion in 'Mac Programming' started by ataylor2009, Jun 2, 2009.

  1. ataylor2009 macrumors member

    Jan 27, 2009
    I'm relatively new to Core Data. I'm trying to piece together a small application using the technology, mainly for learning purposes. What I have is this:

    I have a group of people. Each person carries a phone. Each person answers their phone on a schedule, which they submit in advance. None of the schedules are synchronized (i.e., one person's schedule does not affect any other person's schedule; there may be several people all answering the phone at any given time, or there may be no people answering their phones).

    What I would like to do is display in a Table View a list of all currently active people. In other words, look at the object "Bob," and if "Bob" has volunteered to answer his phone from 3pm to 6pm, and it's currently 4pm, then show "Bob" in the list. If it's currently 230pm, don't show "Bob" in the list (until 3pm, then add him to the list). Lather, rinse, repeat for all people objects.

    Twist: How to handle days of the week (i.e., "Bob" answers the phone from 3pm to 6pm Monday thru Wednesday, but only from 3pm to 5pm Thursday and not at all on Friday, Saturday or Sunday).

    As ever, thanks in advance for any advice you can offer.
  2. HiRez macrumors 603


    Jan 6, 2004
    Western US
    I guess you could make an entity Call Window or something, that stores a start date/time and and end date/time. You could maybe use NSDate/NSCalendar, or to keep it simple you could just use an int (0-6) for the day and an int (0-23) or float for the time, or just a single int containing seconds in a week, or whatever you want for repeatable schedules. The point is it stores the start time and end time range for the window during the week.

    Your Person entity would have a "to many" relationship to these (ie. a person can have multiple call windows). So for example if a person answers the phone from 3-5 every weekday, you would have 5 entries in that person's call window array. Then, to generate your current list, make a custom filtering function on your Person array controller subclass. Run through the array of all persons, and for each of them, run through the array of their call windows, and flag a boolean if the current time falls into any of those ranges.

    There's probably better/more efficient ways to do it, that's just off the top of my head.
  3. ataylor2009 thread starter macrumors member

    Jan 27, 2009

    That's interesting - I hadn't considered just using an int for day / hour / minute settings (i.e. day=1, hour=11, min=30 for Monday 1130am) instead of NSDate or NSCalendarDate or whatever. I was noodling around with all kinds of funky functions to get the "now" date, and then compare it to the "rule" dates, and if it's greater than one but less than the next, do something, etc. Seems like it might be a lot simpler to use int numbers instead. I'll have to mull that over an adult beverage and refactor my half-baked idea.

    Thanks for the input.

    Other interested parties, keep it coming.
  4. ataylor2009 thread starter macrumors member

    Jan 27, 2009
    Okay, so if we assume Monday morning at midnight is the "start of the week," i.e. hour 0, and Sunday night at 11:59 pm is the "end of the week," i.e. hour 167:59, how would you determine whether or not right now, i.e. Friday at 3:18pm is later than, say, Wednesday at noon?

    My logic is: Set up seven constants - one for each day of the week - such that the constant equals the appropriate hour for the week (i.e., kMonday=0, kTuesday=24, kWednesday=48, etc.); get the day of the week from the system (not sure how), and use that to set currentTime (i.e., it's Friday, so currentTime = kFriday = 96); get the time of the day from the system (again, not sure how), convert it to 24-hour format if the system can't provide it in that format, and add it to current time (i.e., currentTime = 96 + 15:18 = 111:18); finally compare currentTime to the target (i.e., if currentTime>60 (which is Wednseday at noon) then do some stuff...).

    I hope to god this is not the most efficient way to do this. I'm sure I'm hovering around the correct answer (I've currently got NSCalendar, NSDate, NSDateComponent, and few others open in different tabs, but none of them leap right out at me as being the correct way to do this), but I'm not there yet.

    As usual, thanks in advance for any advice you can offer.
  5. jared_kipe macrumors 68030


    Dec 8, 2003
    You should probably setup a NSRunLoop and NSTimer that fires a method every min or something.

    That method would get the current time from the clock and then compare it via >s and<s to your conditions.

    NSDate *rightNow = [NSDate date]; // returns date object for current time.
    NSString *rightNowStringForm = [rightNow description];
    // this will be like @"2001-03-24 10:45:32 +0600"
    // now you can use whatever sting parsing methods you want to pick off the pices

    NSString *currentMonth = [rightNowStringForm substringWithRange: NSMakeRange( 5, 2 )];
    // currentMonth is autoreleased, so you might want to retain it if you will be using it for a while
    int month = [currentMonth integerValue];

    Just a start. You could conversely turn your times from your data structure into NSDate's and use the built in comparison classes.

Share This Page