查询数据库时的领域EXC_BAD_ACCESS

时间:2015-08-08 19:55:52

标签: realm

我是新手。我已阅读文档并按照创建和查询数据库中的示例进行操作。每当我运行下面的代码时,它就会崩溃

  

让realm = Realm

我有一个视图控制器,我试图像这样查询领域:

            // Get the default Realm
            let realm = Realm()
            // Query using an NSPredicate
            let predicate = NSPredicate(format: "s3_url = %@", s3_url)
            let medias =  realm.objects(MediaRealm).filter(predicate)

每次我运行这个我得到Thread1:EXC_BAD_ACCESS

  

让realm = Realm()

import UIKit
import Alamofire
import RealmSwift

class ViewController: UIViewController {

    let mediaCollection = MediaCollection()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        loadInitialData()
    }

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

    func loadInitialData(){
        //connect to url
        mediaCollection.fetch(onSuccess:{ self.onEventFetched($0) }, onError:{ self.displayAlert($0)})
    }

    func onEventFetched()->Void{
        println("Got stuff")
        println(mediaCollection.models.count)
        processCollection()
    }

    func processCollection()->Void{

        for media in mediaCollection.models{
            let m = media as! Media
            println(m.s3_url)
            println(m.title)
            if let s3_url = m.s3_url{

                //query database, if it already exists dont download it
                // Get the default Realm
                let realm = Realm()
                // Query using an NSPredicate
                let predicate = NSPredicate(format: "s3_url = %@", s3_url)
                let medias =  realm.objects(MediaRealm).filter(predicate)
                if medias.count == 0 {
                    println("no files found")
                    //downloadFile(s3_url)
                }
            }
        }
    }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这是因为我向数据库添加了新字段而没有运行迁移。 解决方案是删除应用程序并重新开始。