简体   繁体   English

在数据库中为重复事件建模,并使用UILocalNotification触发事件

[英]modeling a repeating event in a database, and using UILocalNotification to fire the event

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. 我不确定这将如何工作,但是我想要的是与iPhone附带的Apple闹钟类似的东西。 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. 根据您的选择,警报将触发一次或重复触发一次。

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. 然后,当创建一个新的警报对象时,我将为选定的时间和选定的日期安排UILocalNotification。 To model that in terms of database objects, I'm not sure what I'm supposed to do. 为了对数据库对象建模,我不确定应该做什么。 I was thinking something like: 我在想类似的东西:

Alarm - (name/string) Day - (Sunday - Saturday/represented by integers 0-6, 1 to many relationship from Alarm to Day) 警报-(名称/字符串)天-(星期日-星期六/由0到1的整数表示,从警报到日期的关系为1)

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. 假设在数据库中可以,那么我不确定应该如何安排UILocalNotifications,因为我认为每个应用程序只能有64个。 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. 我在想,我可以采用某种机制来安排可能的前64个警报,然后在打开应用程序时,它将仅重新安排接下来的64个事件。 Is that how I would do that? 那是我会怎么做的吗? Thanks. 谢谢。

Using 2 entities is overkill. 使用2个实体是多余的。 I would just have the Alarm entity and have a single integer attribute on it to hold the alarm days. 我只需要具有Alarm实体,并在其上具有一个整数属性即可保存警报天数。 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
}

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的功能(例如repeatInterval因此您无需向系统显式添加大量的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM