Today Extension "Unable To Load"

1.2k views Asked by At

I'm using Parse to load data into my Today Extension and then update my Widget text field.

When I run my code there seems to be no problems, in fact to debug the code in my Extension I copied the function into my main application and ran it so I could see output (I know it's possible to debug the widget but it seems like a very confusing and troublesome process). All the output was correct, so I was completely dumbfounded when it did not work in my Widget.

I assume it did not work in my widget Extension because I get the "Unable To Load" text field in my widget view.

The function I use is called "update_widget", and I call "update_widget" in "super.viewDidLoad()" in my Widget "TodayViewController"

Am I updating my widget in the wrong fashion?

Any ideas as to why this might be?

code of "TodayViewController.swift" is below. Assume Parse ID and query ID is correct and not "HiFriends". Assume that the Day class is written and works correctly. Finally keep in mind that if I were to paste "update_widget" in my main Application, "AppDelegate.swift", all the output would be correct and there would be no crashes. Dig in.

Also if it helps, I noticed that before displaying "unable to load", the widget displays "loading" which I print before I query in the "update_widget" function.

    //
    //  TodayViewController.swift
    //  WidgetExtention
    //
    //  Created by Vishnu Murale on 6/9/15.
    //  Copyright (c) 2015 Vishnu Murale. All rights reserved.
    //

    import UIKit
    import NotificationCenter
    import Parse


    class TodayViewController: UIViewController, NCWidgetProviding {

@IBOutlet weak var WidgetText: UILabel!
let BlockOrder = ["A","B","C","D","E","F","G"]


var Schedule = PFObject(className:"Schedule")
let defaults = NSUserDefaults.standardUserDefaults()

override func viewDidLoad() {
    super.viewDidLoad()


    Parse.setApplicationId("HiFriends",
        clientKey: "HiFriends")

   self.update_widget()


}

func find_Minutes(hour_before : Int, hour_after : Int)->Int{


    var num_hours_less = Int(hour_before/100)
    var num_hours_more = Int(hour_after/100)

    var diff_hours = num_hours_more-num_hours_less

    var diff_minutes = hour_after%100 - hour_before%100


    return diff_hours*60 + diff_minutes;


}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func substring(origin :String, StartIndex : Int, EndIndex : Int)->String{
    var counter = 0
    var subString = ""
    for char in origin{

        if(StartIndex <= counter && counter < EndIndex){
            subString += String(char)
        }

        counter++;

    }

    return subString

}

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {

    }


func update_widget() -> Bool{




    var Success = false;

    var Days = [Day]()


    var query = PFQuery(className:"Schedule")

    self.WidgetText.text =  "Loading"

    query.getObjectInBackgroundWithId("HiFriends")
        {
            (SchedObj: PFObject?, error: NSError?) -> Void in
            if error == nil && SchedObj != nil {
                var Widget_Block = [Array<String>]()
                var Time_Block = [Array<String>]()
                Widget_Block.removeAll()
                Time_Block.removeAll()
                println("Retrived Information Successful")


                self.Schedule = SchedObj!
                print(self.Schedule)
                var Monday = Day(name:"Monday")
                var Tuesday = Day(name:"Tuesday")
                var Wednesday = Day(name:"Wednesday")
                var Thursday = Day(name:"Thursday")
                var Friday = Day(name:"Friday")
                if(self.Schedule["MondayBlock"] != nil && self.Schedule["MondayTime"] != nil){
                    var bO: Array<String> = self.Schedule["MondayBlock"] as! Array<String>
                    var t: Array<String> = self.Schedule["MondayTime"] as! Array<String>
                    Widget_Block.append(bO)
                    Time_Block.append(t)
                    Monday.refreshDay(bO, times: t)
                }
                if(self.Schedule["TuesdayBlock"] != nil && self.Schedule["TuesdayTime"] != nil){
                    var bO: Array<String> = self.Schedule["TuesdayBlock"] as! Array<String>
                    var t: Array<String> = self.Schedule["TuesdayTime"] as! Array<String>
                    Widget_Block.append(bO)
                    Time_Block.append(t)
                    Tuesday.refreshDay(bO, times: t)
                }
                if(self.Schedule["WednesdayBlock"] != nil && self.Schedule["WednesdayTime"] != nil){
                    var bO: Array<String> = self.Schedule["WednesdayBlock"] as! Array<String>
                    var t: Array<String> = self.Schedule["WednesdayTime"] as! Array<String>
                    Widget_Block.append(bO)
                    Time_Block.append(t)
                    Wednesday.refreshDay(bO, times: t)
                }
                if(self.Schedule["ThursdayBlock"] != nil && self.Schedule["ThursdayTime"] != nil){
                    var bO: Array<String> = self.Schedule["ThursdayBlock"] as! Array<String>
                    var t: Array<String> = self.Schedule["ThursdayTime"] as! Array<String>
                    Widget_Block.append(bO)
                    Time_Block.append(t)
                    Thursday.refreshDay(bO, times: t)
                }
                if(self.Schedule["FridayBlock"] != nil && self.Schedule["FridayTime"] != nil){
                    var bO: Array<String> = self.Schedule["FridayBlock"] as! Array<String>
                    var t: Array<String> = self.Schedule["FridayTime"] as! Array<String>
                    Widget_Block.append(bO)
                    Time_Block.append(t)
                    Friday.refreshDay(bO, times: t)
                }
                Days.append(Monday)
                Days.append(Tuesday)
                Days.append(Wednesday)
                Days.append(Thursday)
                Days.append(Friday)
                var currentDateTime = Monday.getDate_AsString()
                var Day_Num = Monday.getDayOfWeek_fromString(currentDateTime)
                var User_Info = [String]()
                let defaults = NSUserDefaults.standardUserDefaults()
                if(defaults.objectForKey("ButtonTexts") != nil){
                    User_Info = defaults.objectForKey("ButtonTexts") as! Array<String>
                }
                var Switch_Info = [Bool]()
                if(defaults.objectForKey("SwitchValues") != nil){
                    Switch_Info = defaults.objectForKey("SwitchValues") as! Array<Bool>
                }
                println("")
                println("Finding Widget Date ...")
                var End_Times = [Int]()
                if(self.Schedule["EndTimes"] != nil){
                    End_Times = self.Schedule["EndTimes"] as! Array<Int>
                }


                var minutes_until_nextblock = 0;
                var blockName_Widget = " "

                if(Day_Num >= 0 && Day_Num <= 4){

                    for i in reverse(0...Widget_Block[Day_Num].count-1){



                        var dateAfter = Time_Block[Day_Num][i]
                        var CurrTime = Monday.NSDateToStringWidget(NSDate())
                        var End_Time_String = ""
                        if(i+1 <= Widget_Block[Day_Num].count-1){
                            End_Time_String = Time_Block[Day_Num][i+1]
                        }

                        println("Date After : " + dateAfter)
                        println("Current Date : " + CurrTime)

                        var hour4 = self.substring(dateAfter,StartIndex: 1,EndIndex: 3)
                        hour4 = hour4 + self.substring(dateAfter,StartIndex: 4,EndIndex: 6)

                        var hour2 = self.substring(CurrTime,StartIndex: 1,EndIndex: 3)
                        hour2 = hour2 + self.substring(CurrTime,StartIndex: 4,EndIndex: 6)

                        var end_time = self.substring(End_Time_String,StartIndex: 1,EndIndex: 3)
                        end_time = end_time + self.substring(End_Time_String,StartIndex: 4,EndIndex: 6)


                        var hour_one = hour4.toInt()
                        var hour_two = hour2.toInt()
                        var hour_after = end_time.toInt()


                        println("Blcok  Date  hour : ")
                        print(hour_one)
                        println("Current Date hour: ")
                        print(hour_two)
                        println("After Date  hour : ")
                        println(hour_after)



                        if(i == Widget_Block[Day_Num].count-1 && hour_two >= hour_one){

                            var EndTime = End_Times[Day_Num]
                            if(hour_two! - EndTime < 0){


                                minutes_until_nextblock = self.find_Minutes(hour_two!, hour_after: (EndTime-5))

                                println("Miuntes until next blok " + String(minutes_until_nextblock))
                                if(minutes_until_nextblock > 0){
                                    blockName_Widget = Widget_Block[Day_Num][i]
                                }
                                else{
                                    blockName_Widget = "GETTOCLASS"
                                }
                            }
                            else{
                                println("After School")
                                blockName_Widget = "NOCLASSNOW"
                            }

                            break;

                        }


                        if(hour_two >= hour_one){


                            minutes_until_nextblock = self.find_Minutes(hour_two!, hour_after: (hour_after!-5))

                            println("Miuntes unitl next block " + String(minutes_until_nextblock))

                            if(minutes_until_nextblock > 0){

                                blockName_Widget = Widget_Block[Day_Num][i]
                            }
                            else{
                                blockName_Widget = "GETTOCLASS"
                            }

                            break;
                        }

                    }

                }

                if(blockName_Widget == " "){

                    blockName_Widget = "NOCLASSNOW"

                }



                println("Found the block name Widget " + blockName_Widget )

                if(blockName_Widget == "NOCLASSNOW"){



                    println("NO Class Now")


                }
                else if(blockName_Widget == "GETTOCLASS"){

                    println("Passing Time")

                }
                else{

                    var firstLunch = Switch_Info[Day_Num]

                    for (date,block) in Days[Day_Num].time_to_block{
                        var message = block;
                        var block_copy = block;





                        if(count(block_copy) == 2) {

                            if(firstLunch && block_copy.hasSuffix("1")){


                                message = "Lunch"



                            }
                            else if(!firstLunch && block_copy.hasSuffix("2")){

                                message = "Lunch"


                            }
                            else{

                                var counterDigit = 0;

                                for i in block_copy{

                                    if(counterDigit == 0){
                                        block_copy = String(i)
                                    }

                                    counterDigit++;

                                }



                            }
                        }



                        if(find(self.BlockOrder,block_copy) != nil){

                            var indexOfUserInfo = find(self.BlockOrder,block_copy)!

                            if(User_Info[indexOfUserInfo] != ""){
                                message = User_Info[indexOfUserInfo]

                            }
                        }


                        if(block == blockName_Widget){
                            println("have found widget block " + block)
                            println("Message on Widget " + message)
                            self.WidgetText.text = message
                            break;


                        }

                    }

                }




                Success = true;





            } else {


                println("Information not received succesfully")
            }
    }
    return Success
}

}

1

There are 1 answers

0
Vishnu Murale On

In the end the error was quite simple: I forgot that in order to share between my main App and my Widget I needed to include the suite name in NSUserDefaults. The widget kept getting a null object and crashing.