iOS 今日扩展导致程序以退出代码 : 0 结束

标签 ios swift3 xcode8 today-extension ios10-today-widget

我正在使用 Xcode 8.0、Swift 3。

我正在使今天的扩展显示文本和图像,但经常导致此消息和今天的扩展不起作用。 很少工作。

Program ended with exit code: 0

我将数据保存在领域中,今天使用数据(字符串,图像)进行扩展

是否会导致内存限制?那么,我怎样才能减少对今天使用的扩展的内存呢??

我的扩展代码:

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

    }
}

最佳答案

多半是内存的问题。如果消耗过多,Widget 将被切断。

关于iOS 今日扩展导致程序以退出代码 : 0 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40252266/

相关文章:

ios - 数组数据插入

ios - 从 Swift 2 迁移的项目的 Swift 包管理器

ios - 使用 parse.com 在应用程序中处理推送通知

iphone - UIView 的 CGPoints

xcode - 以编程方式将 View 添加到 UIScrollView

ios - 将代码更新为 Swift 3

Xcode 配置文件位置

ios - 如何在 XCode8 编辑器中恢复按钮字体文本的颜色设置

ios - 缺少 "aps-environment"权利。注册失败

ios - 单元测试提前返回 if 语句