Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weeklyAtTime #428

Open
desmondbaldwin opened this issue May 8, 2020 · 4 comments
Open

weeklyAtTime #428

desmondbaldwin opened this issue May 8, 2020 · 4 comments

Comments

@desmondbaldwin
Copy link

When I create a schedule using weeklyAtATime on a given day, lets use today, Friday May 8th, only the tasks that are created that include a schedule with today's weekday (Weekday 6) are retrieved when I perform
storeManager.store.fetchAnyTask(withID: ........

Using that above date, if I create schedules with every other weekday (1,2,3,4,5,7) on today's date, I get the following error when I query the database:

Error: fetchFailed(reason: "No task with ID: 0BC8130D-FF80-4679-A6FC-C4330865CD1C")

I've confirmed that the ID is correct so I'm not sure why it's not being found for the other weekdays.

When I create the schedule using weeklyAtATime, I use today as the start date, I thought that was the issue but if I use tomorrow as the startdate to create a weekly schedule for something happening every Saturday, the query still does not find the task. I hope I explained this well.

@erik-apple
Copy link
Collaborator

Could you perhaps provide some sample code that helps us reproduce this issue?

@desmondbaldwin
Copy link
Author

// Fuction that is called the save the habit
@objc private func saveAddHabitViewController() {

    //0) Perform error checking and rearrange the times for the schedule if needed
    var myArrayOfTimes: [Date] = []
    let formatter = DateFormatter()
    formatter.dateFormat = "hh:mm a"
    
    inputTime_1.isHidden = true
    inputTime_2.isHidden = true
    inputTime_3.isHidden = true
    inputTime_4.isHidden = true
    inputTime_5.isHidden = true
    inputTime_6.isHidden = true
    inputTime_7.isHidden = true
    inputTime_8.isHidden = true
    inputTime_9.isHidden = true
    inputTime_10.isHidden = true
    
    if inputTime_1.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_1.text!)!)
    }
    if inputTime_2.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_2.text!)!)
    }
    if inputTime_3.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_3.text!)!)
    }
    if inputTime_4.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_4.text!)!)
    }
    if inputTime_5.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_5.text!)!)
    }
    if inputTime_6.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_6.text!)!)
    }
    if inputTime_7.text != ""{
       myArrayOfTimes.append(formatter.date(from: inputTime_7.text!)!)
    }
    if inputTime_8.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_8.text!)!)
    }
    if inputTime_9.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_9.text!)!)
    }
    if inputTime_10.text != ""{
        myArrayOfTimes.append(formatter.date(from: inputTime_10.text!)!)
    }
    
    myArrayOfTimes.sort()
   
    inputTime_2.text = ""
    inputTime_3.text = ""
    inputTime_4.text = ""
    inputTime_5.text = ""
    inputTime_6.text = ""
    inputTime_7.text = ""
    inputTime_8.text = ""
    inputTime_9.text = ""
    inputTime_10.text = ""
    
    for i in 0 ..< myArrayOfTimes.count{
        
        if i == 0 {
            inputTime_1.text =  formatter.string(from: myArrayOfTimes[0])
            inputTime_1.isHidden = false
        }else if i == 1 {
            inputTime_2.text = formatter.string(from: myArrayOfTimes[1])
            inputTime_2.isHidden = false
        }else if i == 2 {
            inputTime_3.text = formatter.string(from: myArrayOfTimes[2])
            inputTime_3.isHidden = false
        }else if i == 3 {
            inputTime_4.text = formatter.string(from: myArrayOfTimes[3])
            inputTime_4.isHidden = false
        }else if i == 4 {
            inputTime_5.text = formatter.string(from: myArrayOfTimes[4])
            inputTime_5.isHidden = false
        }else if i == 5 {
            inputTime_6.text = formatter.string(from: myArrayOfTimes[5])
            inputTime_6.isHidden = false
        }else if i == 6 {
            inputTime_7.text = formatter.string(from: myArrayOfTimes[6])
            inputTime_7.isHidden = false
        }else if i == 7 {
            inputTime_8.text = formatter.string(from: myArrayOfTimes[7])
            inputTime_8.isHidden = false
        }else if i == 8 {
            inputTime_9.text = formatter.string(from: myArrayOfTimes[8])
            inputTime_9.isHidden = false
        }else if i == 9 {
            inputTime_10.text = formatter.string(from: myArrayOfTimes[9])
            inputTime_10.isHidden = false
        }
        
    }
    
    timeInputStepper.value = Double(myArrayOfTimes.count)
    
    //************************************************************
    
    
    //Place current data entered into variables
    
    let mystore = OCKStore(name: "LifeScoreTrackingData")
    let today = Calendar.current.startOfDay(for: Date())
    let auto_ID = UUID()
    auto_ID_Label.text = auto_ID.uuidString
    print("THe Habit ID - ",auto_ID.uuidString)
    
    var currentSchedule: OCKSchedule!
    var finalSetOfSchedules: OCKSchedule!
    var runningListOfSchedules : [OCKSchedule] = []
    
    if daily_Button.isSelected {  //Set up schedule for a dialy habit

        //For each recorded time we will create a schedule and add it to var allTheSchedules
         for i in 0 ..< myArrayOfTimes.count{
            let hour = Calendar.current.component(.hour, from: myArrayOfTimes[i])
            let minutes = Calendar.current.component(.minute, from: myArrayOfTimes[i])
            currentSchedule = OCKSchedule.dailyAtTime(hour: hour, minutes: minutes, start: today, end: nil, text: "", duration: .seconds(0) )
            runningListOfSchedules.append(currentSchedule)
        }
         finalSetOfSchedules = OCKSchedule(composing: runningListOfSchedules)
        
    }else{                         //Set up the schedule for a habit that does not happen daily, e.g. every M, W,F
        
           for i in 0 ..< myArrayOfTimes.count{
              let hour = Calendar.current.component(.hour, from: myArrayOfTimes[i])
              let minutes = Calendar.current.component(.minute, from: myArrayOfTimes[i])   
               
               if sun_Button.isSelected{
                    currentSchedule = OCKSchedule.weeklyAtTime(weekday: 1, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                    runningListOfSchedules.append(currentSchedule)
               }
               if mon_Button.isSelected{
                    currentSchedule = OCKSchedule.weeklyAtTime(weekday: 2, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                    runningListOfSchedules.append(currentSchedule)
               }
               if tue_Button.isSelected{
                    currentSchedule = OCKSchedule.weeklyAtTime(weekday: 3, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                    runningListOfSchedules.append(currentSchedule)
               }
               if wed_Button.isSelected{
                   currentSchedule = OCKSchedule.weeklyAtTime(weekday: 4, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                   runningListOfSchedules.append(currentSchedule)
               }
               if thr_Button.isSelected{
                   currentSchedule = OCKSchedule.weeklyAtTime(weekday: 5, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                   runningListOfSchedules.append(currentSchedule)
                    
               }
               if fri_Button.isSelected{
                currentSchedule = OCKSchedule.weeklyAtTime(weekday: 6, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                    runningListOfSchedules.append(currentSchedule)
                   
               }
               if sat_Button.isSelected{
                    currentSchedule = OCKSchedule.weeklyAtTime(weekday: 7, hours: hour, minutes: minutes, start: today, end: nil, targetValues: [], text: "", duration: .seconds(0))
                    runningListOfSchedules.append(currentSchedule)
                   
               }
            }
             finalSetOfSchedules = OCKSchedule(composing: runningListOfSchedules)
    }
    
    //************************************************************
    
    var newTask = OCKTask(id: auto_ID_Label.text!, title: habit_Title_TextBox.text!, carePlanID: nil, schedule: finalSetOfSchedules)
    newTask.impactsAdherence = true
    newTask.remoteID = String(myArrayOfTimes.count) //using field to capture the number of times each day
    
    //Using OCKNote to store the Reminder Button State to be used by the synchronizer when creating the view
    let newNote = OCKNote(author: "ReminderOff", title: "", content: "")
    newTask.notes = [newNote]
    
    switch mindBodySpirit_SegmentControl.selectedSegmentIndex {
    case 0:
        newTask.groupIdentifier = "Mind"
        
    case 1:
        newTask.groupIdentifier = "Body"
        
    case 2:
        newTask.groupIdentifier = "Spirit"
        
    default:
        newTask.groupIdentifier = "Body"
    }
    
    mystore.addTask(newTask, callbackQueue: .main, completion: nil)
    
    let alertController = UIAlertController(title: "", message:
        "Saved!", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))

    self.present(alertController, animated: true, completion: nil)
    
}

@erik-apple
Copy link
Collaborator

@desmondbaldwin Is this problem still present in the beta of CareKit 2.1?

@desmondbaldwin
Copy link
Author

I am unable to tell because I am unable to use Carkit 2.1 at this time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants