我正在构建一个需要支持重复事件的组日历应用程序,但我提出的处理这些事件的所有解决方案似乎都很简单。我可以限制一个人可以看多远的未来,然后一次性生成所有事件。或者,我可以将事件存储为重复的,并在日历上动态显示它们,但如果有人想更改事件的特定实例的细节,则必须将它们转换为正常事件。
我相信有更好的办法,但我还没找到。对重复发生的事件建模的最佳方法是什么?在这种方法中,您可以更改或删除特定事件实例的细节?
(我使用Ruby,但请不要让这限制了你的回答。如果有ruby特定的库或其他东西,那么最好知道。)
重复事件可能会有很多问题,让我强调一些我所知道的。
解决方案1 -没有实例
存储原始约会+复发数据,不存储所有实例。
问题:
当你需要的时候,你必须在一个日期窗口中计算所有的实例,成本很高
无法处理异常(即。你删除一个实例,或者移动它,或者更确切地说,你不能用这个解决方案这样做)
解决方案2 -存储实例
存储从1开始的所有内容,以及链接回原始约会的所有实例。
问题:
占用很大的空间(但是空间很便宜,所以很小)
必须优雅地处理异常,特别是在做出异常后返回并编辑原始约会时。例如,如果您将第三个实例向前移动一天,那么如果您返回并编辑原始约会的时间,在原始日期重新插入另一个实例,并保留已移动的实例,该怎么办?断开移动的那个?试着适当地改变移动的那个?
当然,如果不打算使用异常,那么任何一种解决方案都可以,基本上可以从时间/空间权衡方案中进行选择。
我开发了多个基于日历的应用程序,还编写了一组支持递归的可重用JavaScript日历组件。我写了一篇关于如何设计递归式的概述这可能对一些人有帮助。虽然有一些建议是针对我所编写的库的,但所提供的绝大多数建议都适用于任何日历实现。
以下是一些要点:
Store recurrence using the iCal RRULE format -- that's one wheel you really don't want to reinvent
Do NOT store individual recurring event instances as rows in your database! Always store a recurrence pattern.
There are many ways to design your event/exception schema, but a basic starting point example is provided
All date/time values should be stored in UTC and converted to local for display
The end date stored for a recurring event should always be the end date of the recurrence range (or your platform's "max date" if recurring "forever") and the event duration should be stored separately. This is to ensure a sane way of querying for events later. Read the linked article for more details about this.
Some discussion around generating event instances and recurrence editing strategies is included
这是一个非常复杂的话题,有很多很多有效的方法来实现它。我要说的是,我实际上已经成功地实现了几次递归,并且我会谨慎地从那些没有实际使用过递归的人那里获得建议。