【问题标题】:UiKit should be called from the main thread only warning应从主线程调用 UiKit 仅警告
【发布时间】:2018-10-09 03:53:09
【问题描述】:

我通过单击按钮将事件添加到日历中。每当我点击那个按钮时,Xcode 都会给我一个警告并挂起应用程序大约几秒钟,然后将事件添加到日历中。警告如下:

void _WebThreadLockFromAnyThread(bool), 0x175bd5c0:从主线程或web线程以外的线程获取web lock。不应从辅助线程调用 UIKit。

我用来添加事件的代码如下:

- (IBAction)btn_reminder_click:(id)sender{

    [self addEventTocalendar];
}

- (void)addEventTocalendar{
    EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        if (btn_appointment.isSelected) {
            event.title = @"Appointment Reminder.";
        }
        else if (btn_pickup.isSelected){
            event.title  = @"Pickup Reminder";
        }

        event.startDate = self.selectedDate;
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];//set 1 hour meeting
        event.notes = txt_notes.text;
        event.recurrenceRules = EKRecurrenceFrequencyDaily;
        [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]];
        if (selectedIndex == 1) {
            [event addRecurrenceRule:[[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 end:Nil]];

        }
        else if (selectedIndex == 2){
            [event addRecurrenceRule:[[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyWeekly interval:1 end:Nil]];
        }
        else if (selectedIndex == 3){
            [event addRecurrenceRule:[[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly interval:1 end:Nil]];
        }
        else if (selectedIndex == 4){
            [event addRecurrenceRule:[[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly interval:1 end:Nil]];
        }
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
        BOOL isSuceess=[store saveEvent:event span:EKSpanThisEvent error:&err];

        if(isSuceess){
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:@"Event added in calendar" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertview show];

        }
        else{
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:[err description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertview show];

        }
    }];

}

cam 有人帮我解决这个问题,因为我无法解决这个问题。

【问题讨论】:

    标签: ios


    【解决方案1】:

    您的 UIAlertView 需要显示在主线程上。试试这个:

    if(isSuceess){
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:@"Event added in calendar" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            [alertview show];
        });
    }
    else {
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:[err description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
           [alertview show];
        });
    }
    

    【讨论】:

      【解决方案2】:

      UI 代码必须始终在主线程上运行。

      您需要将代码分派到主线程。你可以使用这样的东西:

      斯威夫特

      DispatchQueue.main.async 
      {    
          // Your code to execute
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-14
        • 2018-04-11
        • 1970-01-01
        • 1970-01-01
        • 2022-10-04
        • 2013-08-10
        • 1970-01-01
        • 2018-02-23
        相关资源
        最近更新 更多