开发者

How to insert forever repeated events in sqlite3 iphone database?

I have one iphone application in which recurring events happens.the duration are like every day,every 2nd day,every 3rd day..,every week on mon,every week on tues,...,every second week on mon,..,every month on 1sr,every month on 2nd...,every second month on 1st,every second month on 2nd,... For some events there is end date but some events occurs forever.so how can i insert them automat开发者_如何转开发ically in sqlite3 database.eg.If an event repeats every 3rd day.how can i store it automatically after 3 days.or should i store all the events at the time of creation.If i store all the evnets at time of creation then the events that repeats forever.upto what duration i should store the value of them in database.

For this i have thought 2 approaches.one is storing just one occurance with repeated duration like every day.but in my application edit and delete functionality is also there.suppose event has one field event description then it can be different for different dates if user edit the events.events are displayed datewise on screen for a particular month and user can navigate to any previous and next month for current ,next and previous years.So if i use only single occurance then where should those edited or deleted events should be stored. And if i take second approach store each occurance in database.Then upto what duration i should store the events which has no enddate.or is there a way that insert is automatically performed after specified duration.


Two ways, one an easy hack, one more difficult but correct :)

(1) Store each individual event for the next 10 years or so (or however long you want to!)

Good : Easy and quick to implement

Bad : Your db will get big very quickly. What if the user wants to edit the details - how do you edit all of them at once? What if the user wants to cancel / move the event- you have to delete all of them!

(2) Store some sort of 'event recurrence' information in the database with each event i.e. 'Every tuesday'

Good : Only one event in the database, regardless of how many times it repeats. Easy for the user to edit / delete the event - there's only one row in the database.

Bad: More complicated event object - each event must have a list of 'when this event happens' information. Makes very complicated event timings hard - i.e. 'last friday of every month' Takes longer to implement


EDIT : My personal choice

I would choose option (2) - it takes longer to implement but I think option (1) will et you into trouble in the future.

I would have a data model like

Event has many Occurances

where your Event is the thing that the user has created with a description, start date etc and an Occurance is some sort of object that will say 'every friday' or 'not on the 4th'.

As part of creating an event, the user will say 'occurs once on Friday 13th' or 'occurs every Wednesday'. That information is used to create an array of Occurance objects.

Occurance would be a protocol that simply has the method occursOn: so you can have lots of different types of occurance (and you can add new types as your app gets more complicated).

The Event object would have a method something like occursOn: where you give it an NSDate and it returns if it occurs on that day. It does this by asking each of it's occurances in turn to see if they apply to that day.

To deal with deleted events, just add an Occurance to the end of the Event's array that overrides the others - i.e. 'not on Friday 13th'.

For example :

(1)

A user creates an event called 'My Event' starting on 1st Jan, occurring every Friday.

Your app would store

Event
    description : 'My Event',
    start date : 1st Jan 2011
    occurances :
        WeeklyOccurance
            day : Friday

where WeeklyOccurance implements the <Occurance> protocol

(2)

The user asks to show the week's events, starting on Sunday the 8th Jan 2011

The app would :

For each day in the week
    For each event in the database
        if occursOn: this day
            show the event on the ui

and for our event 'My Event', it would implement occursOn: like

- (BOOL)occursOn:(NSDate *)date

    is this date before this event starts
        if it is, return NO

    set remembered = NO
    for each occurance
       does this occurance say 'yes','no' or '?' for this date?

       if 'yes' set remembered YES
       if 'no' return NO

       if '?' continue the loop

   return remembered

Because WeeklyOccurace only knows that it occurs on Fridays, it would return 'yes' for Fridays and '?' for all other days so the ui would show 'My Event' on Friday and not on any other days.

To add different types of occurance, just implement the <Occurance> protocol in different ways.

(3)

The user says actually it should be on every Friday apart from the 22nd

The app would create another Occurance, this time a NotOnThisDayOccurance and add it to the end of the Event's array i.e.

Event
    description : 'My Event',
    start date : 1st Jan 2011
    occurances :
        WeeklyOccurance
            day : Friday
        NotOnThisDayOccurance
            day: 22nd Jan 2011

Now, if the users asks to display the weekly events, 'My Event' would look do this :

Ask the WeeklyOccurance if it's valid for friday the 22nd - this would return yes.
Ask the NotOnThisDayOccurance if it's valid for friday the 22nd - this would override the previous result and say NO

Therefore, the event would not show up on the 22nd but would show up on all the other fridays.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜