在数据库中为重复事件建模,并使用UILocalNotification触发事件 [英] modeling a repeating event in a database, and using UILocalNotification to fire the event

查看:73
本文介绍了在数据库中为重复事件建模,并使用UILocalNotification触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这将如何工作,但是我想要的是与iPhone附带的Apple闹钟类似的东西。基本上,它只是让您选择警报的时间,命名警报,然后可以选择重复的频率(星期日-星期六)。根据您的选择,警报将触发一次或重复触发一次。

I'm not sure how this would work, but what I'd like is something similar to Apple's alarm clock that comes with the iPhone. It basically just lets you pick a time of an alarm, name the alarm, and then you can choose how often you want it to repeat (Sunday - Saturday). Based on what you choose, the alarm fires once, or at a repeated interval.

在我的核心数据模型中,我不确定如何建模。如果我只考虑普通的旧对象,那么我会认为我会有一些警报对象,并且其属性之一是数组。在该数组中,我可以具有周日至周日的日值。然后,当创建一个新的警报对象时,我将为选择的时间和选择的日期安排UILocalNotification。要根据数据库对象建模,我不确定该怎么做。我在想类似的东西:

In my Core Data model, I wasn't sure how to model that. If I were thinking in terms of just plain old objects, I would think I would have some alarm object, and one of its properties would be an array. In that array I could have the day values of Sunday-Sautrday. Then when a new alarm object is created, I would schedule a UILocalNotification for the time selected, and the days chosen. To model that in terms of database objects, I'm not sure what I'm supposed to do. I was thinking something like:

警报-(名称/字符串)
日-(星期日-星期六/由0到1的整数表示,1对多的关系

Alarm - (name/string) Day - (Sunday - Saturday/represented by integers 0-6, 1 to many relationship from Alarm to Day)

假设在数据库中没有问题,那么我不确定应该如何安排UILocalNotifications,因为我认为您只能有64个每个应用。我认为我可以采用某种机制来安排可能的前64个警报,然后在打开应用程序时,它将仅重新安排接下来的64个事件。那是我会怎么做的吗?谢谢。

Assuming that is ok in the database, then I'm not sure how I should go about scheduling the UILocalNotifications since I thought you could only have 64 per app. I'm thinking that I could have some mechanism to schedule the first 64 alarms possible, then when the app is opened, it would just reschedule the next upcoming 64 events. Is that how I would do that? Thanks.

推荐答案

使用2个实体实在是太过分了。我只需要具有 Alarm 实体,并在其上具有一个整数属性即可保存警报天数。在实体外部,我将有一个枚举,该枚举定义了如何解释警报天数。

Using 2 entities is overkill. I would just have the Alarm entity and have a single integer attribute on it to hold the alarm days. Outside of the entity, I would have an enumeration which defines how the alarm days number is interpreted. Something like:

typedef AlarmDays {
    Monday           = 0,
    Tuesday          = 1 << 0,
    Wednesday        = 1 << 1,
    Thursday         = 1 << 2,
    Friday           = 1 << 3,
    Saturday         = 1 << 4,
    Sunday           = 1 << 5
} AlarmDays;

然后,您可以使用以下方法测试日期:

Then you can test which days it should be on using:

if (alarm.alarmDays & Monday) {
    // the alarm should fire on mondays
}

您可以使用 UILocalNotification 的功能,例如 repeatInterval ,因此您无需向系统显式添加大量的通知。

And you can use the features of UILocalNotification, such as repeatInterval so you don't need to explicitly add gazillions of notifications to the system.

这篇关于在数据库中为重复事件建模,并使用UILocalNotification触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆