如何在sqlite3 iphone数据库中插入永远重复的事件?
我有一个iPhone应用程序,其中会发生重复事件。持续时间就像每天,每第二天,每第三天..,每周一,每周二,...,每第二周周一,.. ,每个月的 1 日,每个月的 2 日...,每第二个月的 1 日,每第二个月的 2 日,... 对于某些事件有结束日期,但有些事件永远发生。那么如何才能我将它们自动插入到 sqlite3 数据库中。例如,如果一个事件每 3 天重复一次。我如何在 3 天后自动存储它。或者我应该在创建时存储所有事件。如果我在创建然后永远重复的事件。我应该将它们的值存储在数据库中的持续时间。
为此,我想到了两种方法。一种是仅存储一次重复持续时间的事件,例如每天。但在我的应用程序中,编辑和删除功能也存在。假设事件有一个字段事件描述,那么如果用户在不同的日期,它可能会有所不同编辑事件。特定月份的事件按日期显示在屏幕上,用户可以导航到当前、下一年和上一年的任何上个月和下个月。因此,如果我仅使用单个事件,那么这些编辑或删除的事件应该存储在哪里。 如果我采取第二种方法将每个事件存储在数据库中。那么我应该存储没有结束日期的事件的持续时间。或者是否有一种方法可以在指定的持续时间后自动执行插入。
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 automatically 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法,一种是简单的破解,一种更困难但正确:)
(1) 将每个单独的事件存储在接下来的 10 年左右(或者无论你想要多久!)
好:
实施起来简单快捷
坏处:
你的数据库会很快变大。
如果用户想要编辑详细信息怎么办 - 如何一次编辑所有详细信息?
如果用户想要取消/移动事件怎么办 - 您必须将其全部删除!
(2) 在数据库中存储某种“事件重复”信息以及每个事件,即“每个星期二”
好:
数据库中只有一个事件,无论它重复多少次。
用户可以轻松编辑/删除事件 - 数据库中只有一行。
坏的:
更复杂的事件对象 - 每个事件都必须有一个“此事件发生时”信息的列表。
使非常复杂的事件计时变得困难 - 即“每个月的最后一个星期五”
需要更长的时间来实施
编辑:我个人的选择
我会选择选项(2) - 需要更长的时间来实施,但我认为选项(1)会让你在未来陷入麻烦。
我会有一个像
Event has many Occurances
这样的数据模型,其中您的 Event 是用户使用描述、开始日期等创建的内容,而 Occurance 是某种对象,会显示“每个星期五” ”或“不在 4 号”。
作为创建事件的一部分,用户会说“13 号星期五发生一次”或“每周三发生一次”。该信息用于创建 Occurance 对象数组。
Occurance 将是一个仅具有方法
occurrsOn:
的协议,因此您可以拥有许多不同类型的发生(并且当您的应用程序变得更加复杂时,您可以添加新类型)。Event 对象将有一个类似于
occurrsOn:
的方法,您可以在其中给它一个NSDate
,如果它发生在那天,它就会返回。它通过依次询问每个事件的发生来查看它们是否适用于当天来实现这一点。要处理已删除的事件,只需将 Occurance 添加到事件数组的末尾即可覆盖其他事件 - 即“不在 13 号星期五”。
例如:
(1)
用户创建一个名为“我的事件”的事件,从 1 月 1 日开始,每周五发生。
您的应用程序将存储
WeeklyOccurance 实现
协议的位置(2)
用户要求显示从 2011 年 1 月 8 日星期日开始的本周事件
应用程序将:
对于我们的事件“我的事件” ',它会实现 attemptsOn: 就像
因为 WeeklyOccurace 只知道它发生在星期五,所以它会在星期五返回 'yes' 和 '?'对于所有其他日期,用户界面将在星期五显示“我的活动”,而不是在任何其他日期。
要添加不同类型的发生,只需以不同的方式实现
协议即可。(3)
用户说实际上应该是除了 22 日之外的每个星期五
应用程序将创建另一个 Occurance,这次是 NotOnThisDayOccurance 并将其添加到事件数组的末尾,即
现在,如果用户要求显示每周事件,“我的活动”看起来会这样做:
因此,该活动不会出现在 22 日,但会出现在所有其他星期五。
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 anNSDate
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
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 :
and for our event 'My Event', it would implement occursOn: like
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.
Now, if the users asks to display the weekly events, 'My Event' would look do this :
Therefore, the event would not show up on the 22nd but would show up on all the other fridays.