How would you design this algorithm?

Discussion in 'iOS Programming' started by ataylor2009, Mar 9, 2010.

  1. ataylor2009 macrumors member

    Joined:
    Jan 27, 2009
    #1
    I'm working on an app that performs a task based on a schedule. Briefly, the user will be able to set up some available times - say, Monday 8am-2pm, Tuesday through Friday 9am-5pm, and Saturday 8am-8pm. Then, when the magic button is pressed, the app should find the appropriate task and do it based on the current time. In other words, based on the samples above, if I press the button at 11am on Monday, it should do whatever is in the "Monday 8am-2pm" slot. If I press the button at, say, 6pm on Monday, then the app should do a default action since no event has been assigned at that time.

    I think I can handle the coding; what I can't come up with is a reasonable algorithm for having the app determine which action to perform. Any tips, suggestions or thoughts on the logic here will be appreciated.
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    Do you really need any kind of special algorithm? It depends on how you're persisting the data, but if you're using a sqlite database, then surely a simple SQL query would suffice?
     
  3. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #3
    I don't know what I need. My plan is to use Core Data for the storage. I'm assuming my entity will have a "Name" attribute, plus "Start" and "End" attributes. The name will be an NSString, and the start and end will be NSDate objects.

    It seems like I need some kind of comparison between the current date (when the button is pressed) and the date objects in the associated event, so that either the currently "active" event is performed, or, in the case that there is no currently "active" event, a default event is performed instead.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    "NSDate objects represent a single point in time." That's the first sentence in NSDate's reference doc.

    Monday at 11:30 am is not a single point in time. It's a recurring or circular point in time, which occurs every week, on Mondays.

    What if you encoded days of the week as integers 1-7. Then encode the time of day as hours and minutes. Multiply day-of-week by 10000, add time of day, now you have a pure number that you can do things with. For example, Mon is day 2, 11:30 am is 1130. Multiply 2 by 10000 and add 1130 to get 21130.

    Given pure numbers, you can look at NSRange and the various things that operate on NSRange, or allow you to create sets of ranges.
     
  5. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #5
    Let me see if I follow.

    When a rule is entered, say Wednesday 9am-2pm, a starting value (3*10000+900=30900) and an ending value (3*10000+1430=31430) are calculated.

    When the magic button is pushed, another value is calculated (Tuesday 2:55pm = 2*10000+1455=21455). The new value is compared to the starting and ending values of the rules (however many there are), and if it falls within the range of any rule, that rule is executed. If it does not fall within the range of any rule, then a default action occurs.

    That seems like a simple, workable solution, if I understood you correctly.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    That is what I meant.

    It's unclear what you want to happen if the button isn't pressed during one interval, say Wed-9am-2pm, and is pressed on Thur 11 pm. Should the Wed task run because it's been postponed, or only the Thur task?

    2pm would be 1400, not 1430.
     
  7. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #7
    Been a long time since I was in the Marines...I forgot how to tell time in military.

    There will be a default task. If the user selects "Do yardwork" Thursday at 11pm, and the only yardwork rule is valid on Wednesday from 9am-2pm, then the default task ("Soak cat in Miracle Whip") will be executed instead.
     
  8. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #8
    I believe what chown33 means is that NSDate is based on time intervals, not relative weekly times. For instance, at the time of this posting, NSDate returns 1268216677, which is the number of milliseconds since the Unix epoch.

    This means that for recurring things you'll need to create your own level of abstraction to test that a given NSDate is in the range "Wednesday 9am-2pm". Once you have this, SQL, as Luke Redpath suggested, is likely the simplest option.

    FMDB provides nice wrappers for SQLite on iPhone.
     
  9. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #9
    Okay. The algorithm/formula presented above (day# * 10k + time) seems to work just fine. That will do for the comparison I need to perform.

    I'm not following why the suggestions for SQL, though...that seems like an unnecessarily complex way to store the objects I've created. Wouldn't Core Data be the simpler way to go? Each entity would have a name and an array of rules (or an array of dictionaries with start and end values calculated by the formula above), and I could then fetch only those rules that are relevant.

    Am I missing something here?
     

Share This Page