iOS:应用程序在获取请求后崩溃EXC_BAD_ACCESS

时间:2017-08-08 12:05:07

标签: ios swift3

我有UITiewView的UIViewController。当用户从表中选择任务然后按钮保存任务时,它应该通过taskRelationship保存到Day实体。但是,应用程序崩溃与exc_bad_access,我找不到原因。

控制器代码:

class ActivityTodayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

@IBOutlet weak var activityToday: UITableView!

let moc = DatabaseController.getContext()

@IBAction func addActivityToDay(_ sender: Any) {
    //DEFINE BUTTON ACTION

    //FIND SELECTED TASK
    let indexPath = activityToday.indexPathForSelectedRow
    let task = fetchedResultsController.object(at: indexPath!) as! Task

    //GET LAST DAY
    let fetchRequest:NSFetchRequest<CalendarDays> = CalendarDays.fetchRequest()
    let sort = NSSortDescriptor(key: #keyPath(CalendarDays.oneDay), ascending: false)
    fetchRequest.sortDescriptors = [ sort ];
    fetchRequest.fetchLimit = 1;

    do {
        let lastDay = try moc.fetch(fetchRequest)

        //ADD TASK TO LAST DAY        
        lastDay[0].addToTaskRelationship(task) //it crashes here
        DatabaseController.saveContext()

    } catch {
        print("Cannot fetch Expenses")
    }

    //MOVE TO TODAYS VIEW
    navigationController?.popToRootViewController(animated: true)
}


override func viewDidLoad() {
    super.viewDidLoad()

    activityToday.delegate=self;
    activityToday.dataSource=self;

    initializeFetchedResultsController()
}


//FOR DISPLAYING DATA IN TABLE
var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>!

func initializeFetchedResultsController() {
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Task")
    let tasktSort = NSSortDescriptor(key: "taskdes", ascending: true)
    let posNegSort = NSSortDescriptor(key: "positive", ascending: false)
    request.sortDescriptors = [posNegSort, tasktSort]

    fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
    fetchedResultsController.delegate = self

    do {
        try fetchedResultsController.performFetch()
    } catch {
        fatalError("Failed to initialize FetchedResultsController: \(error)")
    }
}


//CONSTRUCT TABLE VIEW
//... regular table view methods

当调试lastDay[0].addToTaskRelationship(task)时,它实际上有两个值 - 1表示白天,1表示任务。

带有addToTaskRelationship的CalendarDays代码

extension CalendarDays {

        @nonobjc public class func fetchRequest() -> NSFetchRequest<CalendarDays> {
            return NSFetchRequest<CalendarDays>(entityName: "CalendarDays")
        }

        @NSManaged public var oneDay: Date?
        @NSManaged public var taskRelationship: NSOrderedSet?

    }

    // MARK: Generated accessors for taskRelationship
    extension CalendarDays {

        @objc(addTaskRelationshipObject:)
        @NSManaged public func addToTaskRelationship(_ value: Task)

        @objc(removeTaskRelationshipObject:)
        @NSManaged public func removeFromTaskRelationship(_ value: Task)

        @objc(addTaskRelationship:)
        @NSManaged public func addToTaskRelationship(_ values: NSOrderedSet)

        @objc(removeTaskRelationship:)
        @NSManaged public func removeFromTaskRelationship(_ values: NSOrderedSet)

    }

任何建议都赞赏!

0 个答案:

没有答案