Ios8 由于一个HMActionSet不能同时添加到HMHome和HMTrigger中,如何将HMActionSet添加到HMTrigger中?

Ios8 由于一个HMActionSet不能同时添加到HMHome和HMTrigger中,如何将HMActionSet添加到HMTrigger中?,ios8,xcode6,homekit,Ios8,Xcode6,Homekit,我的代码是: // Create Action Set [_myHome addActionSetWithName:@"Night" completionHandler:^(HMActionSet *actionSet, NSError *error) { if (error) { NSLog(@"%@", error); }else { NSLog(@"Add action set"); } }]; // Create Time Tri

我的代码是:

// Create Action Set
[_myHome addActionSetWithName:@"Night" completionHandler:^(HMActionSet *actionSet, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    }else {
        NSLog(@"Add action set");
    }
}];

// Create Time Trigger
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.second = 5;
HMTimerTrigger *timeTrigger = [[HMTimerTrigger alloc] initWithName:@"Night Trigger" fireDate:[NSDate dateWithTimeIntervalSinceNow:5] timeZone:[NSTimeZone localTimeZone] recurrence:dateComponents recurrenceCalendar:[NSCalendar currentCalendar]];

// Add Action Set to Trigger
for (HMActionSet *actionSet in _myHome.actionSets) {
    if ([actionSet.name isEqualToString:@"Night"]) {
        [timeTrigger addActionSet:actionSet completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }else {
                NSLog(@"Add Action Set to Trigger");
            }
        }];
    }
}

// Add Trigger to My Home
[_myHome addTrigger:timeTrigger completionHandler:^(NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    }else {
        NSLog(@"Add Trigger");
    }
}];
首先,我在HMHome中添加了一个名为“夜”的HMActionSet。然后我将相同的HMActionSet添加到HMTrigger。HomeKit抛出错误:error Domain=HMErrorDomain Code=12“该操作无法完成。(HMErrorDomain error 12)。”这意味着对象已与主机关联

我的猜测是,您不能将相同的HMActionSet添加到HMHome和HMTrigger。所以我尝试创建一个HMActionSet并将其添加到HMTrigger,然后将HMTrigger添加到HMHome。但是我在创建HMActionSet时遇到了一个问题,因为它无法启动


有人能解决这个问题吗

您应该在成功添加actionset后调用add触发器,这意味着在完成处理程序中。

如果已经将HMActionSet添加到home,则可以将其添加到HMTrigger中

HMActionset是一个场景(任何附件的可写特征的集合)。 它可以通过单个命令执行

因此,要么执行HMActionSet,要么将其添加到触发器 在特定时间执行该场景

应该有两个不同的视图来处理HMActionSet(添加、重命名、删除、更改特征值)

对于触发器,它将创建一个触发器,并将HMActionSet从同一个主位置“分配”到该触发器

但是,您需要管理重复性、firedate并使属性能够完美地工作。

您的问题:

dateComponents.second = 5;
计时器触发器仅在一分钟开始时设置不使用秒,如果启动日期包含秒值而不是0,则会返回错误。计时器启动时,通常会在计划启动日期或计算的重复启动日期后1分钟内启动,具体取决于系统电源和资源管理


还有一个注意事项-firedate必须四舍五入到秒。根据苹果的文档:计时器触发器只在一分钟开始时设置。不使用秒,如果启动日期包含除0以外的秒值,则将返回错误。计时器启动时,通常会在计划启动日期或计算的重复启动日期后1分钟内启动,具体取决于系统电源和资源管理。