iOS today extension causes Program ended with exit code: 0
Asked Answered
C

2

7

I'm using Xcode 8.0, Swift 3.

I'm making today extension showing text and image, but frequently cause this message and today extension does not work. rarely work.

Program ended with exit code: 0

I saved data in realm, and today extension using the data(string, image)

is it cause a memory limit? so, how can i derease memory about today extension using??

My extension code:

class Information: Object {

    dynamic var Name = ""
    dynamic var Image : NSData?

    override class func primaryKey() -> String {
        return "Name"
    }
}

class TodayViewController: UIViewController, NCWidgetProviding {

    var realm : Realm?
    var results : Results<Information>?
    var index = 0

    @IBOutlet var NameLabel: UILabel!
    @IBOutlet var ImageView: UIImageView!

    @IBAction func leftAction(_ sender: UIButton) {
        guard index == 0 else {
            index -= 1
            loadData(index: index)
            return
        }
    }

    @IBAction func rightAction(_ sender: UIButton) {
        guard index == (results?.count)! - 1 else {
            index += 1
            loadData(index: index)
            return
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setRealmFileURL()

        if #available(iOSApplicationExtension 10.0, *) {
            extensionContext?.widgetLargestAvailableDisplayMode = .expanded
        }else{
            preferredContentSize = CGSize(width: 0, height: 250)
        }
        // Do any additional setup after loading the view from its nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        realm = try! Realm()
        results = try! Realm().objects(Information.self) 
        loadData(index: index)

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

    private func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -> Void)) {
        // Perform any setup necessary in order to update the view.

        // If an error is encountered, use NCUpdateResult.Failed
        // If there's no update required, use NCUpdateResult.NoData
        // If there's an update, use NCUpdateResult.NewData
        loadData(index: index)

        completionHandler(NCUpdateResult.newData)
    }

    func loadData(index: Int){

        guard results?.count == 0 else {

            let objects = results?[index]
            NameLabel.text = objects?.Name
            ImageView.image = UIImage(data: (objects?.Image)! as Data, scale: 0.1)
            return

        }
    }

    @available(iOSApplicationExtension 10.0, *)
    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
        preferredContentSize = CGSize(width: 0, height: 250)
    }

    func setRealmFileURL(){

        var config = Realm.Configuration(encryptionKey: getKey() as Data)
        let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.xxx")
        let realmPath = directory?.appendingPathComponent("xxx.realm")
        config.fileURL = realmPath

        Realm.Configuration.defaultConfiguration = config

    }
}
Catalepsy answered 26/10, 2016 at 1:25 Comment(0)
P
2

mostly it is a problem with memory. The Widget will get cut off if it consumes to much.

Poi answered 15/6, 2017 at 11:44 Comment(1)
how many memory the widget extensions could take? Less is better I know but on my iPad Pro last gen I currently use 25Mb and widget stop working immediately.Glidden
D
0

Unless something in the app needs to change, don't use

completionHandler(NCUpdateResult.NewData)

instead use

completionHandler(NCUpdateResult.noData)
Dd answered 4/3, 2018 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.