Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UiKit should be called from the main thread only warning

Tags:

ios

I am adding an event to a calendar on the click of a button. Whenever I click on that button the Xcode gives me a warning and hangs the app for about few seconds and then add the event into the calendar. the warning is as follows:

void _WebThreadLockFromAnyThread(bool), 0x175bd5c0: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.

the code which I am using for adding the event is as follows:

- (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 Somebody help me o this as I am not able to solve this problem.

like image 586
Shikhar Avatar asked Dec 03 '25 04:12

Shikhar


1 Answers

Your UIAlertView needs to be shown on the main thread. Try this:

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];
    });
}
like image 171
Joel Avatar answered Dec 04 '25 19:12

Joel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!