TableView节更新取决于CoreData临时属性更改 [英] TableView section update depend on CoreData transient property change

查看:140
本文介绍了TableView节更新取决于CoreData临时属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个时间表应用程序。



我有TableViewController,显示当天的类。在viewDidLoad()中我使用NSFetchedResultsController获取类:

  let fetchRequest = NSFetchRequest(entityName:Lessons)
let predicate = NSPredicate(format:startsday ='\(dateFormatter.stringFromDate(NSDate()))'',NSDate())
fetchRequest.predicate = predicate
let sortDescriptorStarts = NSSortDescriptor begin,ascending:true)
let sortDescriptorTitle = NSSortDescriptor(key:title,ascending:true)
fetchRequest.sortDescriptors = [sortDescriptorStarts,sortDescriptorTitle]
fetchRequest.fetchBatchSize = 30
fetchedResultsController = NSFetchedResultsController(fetchRequest:fetchRequest,managedObjectContext:moc,sectionNameKeyPath:sectionIdentifier,cacheName:nil)
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)

当我设置sectionNameKeyPath:sectionIdentifier(这是一个瞬态属性)时,它们被分为三组过去现在,下一步取决于当前时间。它是工作。我在这里有屏幕截图



我的问题是:随着时间的推移,这些部分不会更新

行应该移出并移动到其他部分,最终他们都应该进入

我可以更新sectionIdentifier transient属性,但是我的fetchedResultController没有' t想要注意到变化...



我不想使用tableView.reloadData(),因为我想要动画。



(存在类似问题,但是)



谢谢!

解决方案

我找到了一个这个问题的解决方案。它绝对不是最好的,但它的工作很好。 (Swift 2.3)



首先,我有一个称为sectionIdentifier的瞬态属性。这可能是NOW,NEXT和PAST取决于当前时间。我在这个称为sectionID的瞬态旁边添加了一个新的持久性属性。此属性的目的是告诉课程状态(NOW,NEXT,PAST)是否更改。我将原始抓取更改为:

  let fetchRequest = NSFetchRequest(entityName:Lessons)
let predicate = NSPredicate(format:startsday ='\(dateFormatter.stringFromDate(NSDate()))',NSDate())
fetchRequest.predicate = predicate
let sortDescriptorStarts = NSSortDescriptor(key:starts ,ascending:true)
let sortDescriptorTitle = NSSortDescriptor(key:title,ascending:true)
fetchRequest.sortDescriptors = [sortDescriptorStarts,sortDescriptorTitle]
fetchRequest.fetchBatchSize = 30
fetchedResultsController = NSFetchedResultsController(fetchRequest:fetchRequest,managedObjectContext:moc,sectionNameKeyPath:sectionIdentifier,cacheName:nil)
fetchedResultsController.delegate = self
do {
try fetchedResultsController.performFetch b} catch _ {
//处理错误
}
如果let lessons = fetchedResultsController.fetchedObjects as? [Lessons] {
for lessons in lessons {
if lesson.sectionID!= lesson.sectionIdentifier! {
lesson.setValue(lesson.sectionIdentifier !, forKey:sectionID)
moc.refreshObject(lesson,mergeChanges:true)
}
}
}

在提取后,我将此persistent属性设置为transient的值。 (上面8行)



在viewDidLoad()中添加了这一行。

  timer = NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,selector:#selector(TodayViewController.update),userInfo:nil,repeates:true)

每秒更新我的瞬态属性。
我的更新方法如下所示:

  func update(){
for lesson in fetchedResultsController.fetchedObjects如! [Lessons] {
if lesson.sectionID!= lesson.sectionIdentifier! {
lesson.setValue(lesson.sectionIdentifier !, forKey:sectionID)
moc.refreshObject(lesson,mergeChanges:true)
do {
try moc.save
}
catch _ {
//处理错误
}
}
}
}

所以我的transient属性将总是给予课程当前状态,持久的将始终给tableviewcell的状态。如果有差异我把我的瞬态值到持久性属性和moc.refreshobject()我会得到一个正确的FetchedResultControllerDelegate方法,我可以处理tableview的变化。


I'm trying to develop a timetable app.

I have TableViewController which shows the classes on current day. In viewDidLoad( ) I fetch the classes with NSFetchedResultsController:

let fetchRequest = NSFetchRequest(entityName: "Lessons")
let predicate = NSPredicate(format: "startsday = '\(dateFormatter.stringFromDate(NSDate()))'", NSDate())
fetchRequest.predicate = predicate
let sortDescriptorStarts = NSSortDescriptor(key: "starts", ascending: true)
let sortDescriptorTitle = NSSortDescriptor(key: "title", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptorStarts, sortDescriptorTitle]
fetchRequest.fetchBatchSize = 30
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)

And when I set sectionNameKeyPath: "sectionIdentifier"(which is a transient property) they are sorted into three groups "Past", "Now", "Next" depend on current time. It is working. I have a screenshot here.

My problem is: As time goes by the sections aren't updated
The rows should move out and move in other sections and eventually they should all go into the "Past" section, and the "Now", "Next" sections should be deleted.

I can update the sectionIdentifier transient property, but my fetchedResultController doesn't want to notice the changes...

I don't want to use tableView.reloadData( ) because i want animations.

(There is a similar question, but the end of it is different. I can't get my answer.)

Thanks!

解决方案

I have found a solution for this problem. It is definitely not the best but it's working nicely. (Swift 2.3)

First, I have a transient property called "sectionIdentifier".This could be "NOW", "NEXT" and "PAST" depend on the current time.I added a new persistent property next to this transient called "sectionID". This property's purpose is to tell if the lesson status (NOW, NEXT, PAST) changed. I changed my original fetch to this:

let fetchRequest = NSFetchRequest(entityName: "Lessons")
let predicate = NSPredicate(format: "startsday = '\(dateFormatter.stringFromDate(NSDate()))'", NSDate())
fetchRequest.predicate = predicate
let sortDescriptorStarts = NSSortDescriptor(key: "starts", ascending: true)
let sortDescriptorTitle = NSSortDescriptor(key: "title", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptorStarts, sortDescriptorTitle]
fetchRequest.fetchBatchSize = 30
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
fetchedResultsController.delegate = self
do {
    try fetchedResultsController.performFetch()
} catch _ {
   //Handle error
}
if let lessons = fetchedResultsController.fetchedObjects as? [Lessons] {
    for lesson in lessons {
        if lesson.sectionID != lesson.sectionIdentifier! {
            lesson.setValue(lesson.sectionIdentifier!, forKey: "sectionID")
            moc.refreshObject(lesson, mergeChanges: true)
        }
    }
}

After the fetch I set this persistent property to the transient's value. (last 8 line above)

In viewDidLoad() I added this line.

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(TodayViewController.update), userInfo: nil, repeats: true)

I update my transient property every second. My update method looks like this:

func update() {
    for lesson in fetchedResultsController.fetchedObjects as! [Lessons] {
        if lesson.sectionID != lesson.sectionIdentifier! {
            lesson.setValue(lesson.sectionIdentifier!, forKey: "sectionID")
            moc.refreshObject(lesson, mergeChanges: true)
            do {
                try moc.save()
            }
            catch _ {
                //Handle error
            }
        }
    }
}

So my transient property will always give the lessons current status and the persistent one will alway give the tableviewcell's status. If there is difference I put my transient value into the persistent property and with moc.refreshobject() i will get a the proper FetchedResultControllerDelegate method and i can handle the tableview changes there.

这篇关于TableView节更新取决于CoreData临时属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆