如何按日期作为分组从核心数据中获取数据?

3
 fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath:"time", cacheName: nil)

我正在使用这段代码,其中time是存储Date()对象的列名。

我想根据日期获取部分内容,例如:

2016年11月29日,总行数=3

2016年11月28日,总行数=1

但目前对于同一日期上的每个时间差异都会获得单独的部分。

那么,如何使用fetch request controller来实现呢???

提前感谢您的帮助。


如果您不使用小时、分钟和秒,最好将其存储为date(例如:29-11-2016)在core data中。这样,您的数据将基于日期进行分组。 - Karthick Selvaraj
你的意思是只存储日期而不存储时间,对吗? - Chandan
是的,完全正确。 - Karthick Selvaraj
我尝试将字符串日期保存为“29-NOV-2016”字符串,它给出了实际结果:29-11-2016 总行数=328-11-2016 总行数=1但是没有按日期排序。 - Chandan
有线索如何根据日期获取章节吗? - Chandan
1个回答

0

这个问题在stackoverflow上已经有解决方案了。请检查它,它会起作用。

import UIKit
import CoreData

class ExampleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

    @IBOutlet weak var tableView: UITableView!

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
    var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()

    var orders = [Order]()
    var startDate : NSDate = NSDate()
    var endDate : NSDate = NSDate()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchedResultController.delegate = self
        tableView.dataSource = self
        tableView.delegate = self

        fetchData()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return fetchedResultController.fetchedObjects!.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let order = fetchedResultController.fetchedObjects![section] as! Order
        return ((order.products?.count ?? 0) + (order.services?.count ?? 0))
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let textCellIdentifier = "ExampleTableViewCell"
        let row = indexPath.row

        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! ExampleTableViewCell

        let order = fetchedResultController.fetchedObjects![indexPath.section] as! Order // Data fetched using NSFetchedResultsController
        let products = (order.products?.allObjects ?? [Product]()) as! [Product] // Swift Array
        let services = (order.services?.allObjects ?? [Service]()) as! [Service] // Swift Array

        if (row < products.count) { // this is a Product row
            cell.orderLabel.text = products[row].name!
        } else { // this is a Service row
            cell.orderLabel.text = services[row-products.count].name!
        }
        return cell
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let order = fetchedResultController.fetchedObjects![section] as! Order
        return "\(order.date)"
    }


    func orderFetchRequest() -> NSFetchRequest {
        let fetchRequest = NSFetchRequest(entityName: "Order")
        let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
        let predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate) // startDate and endData are defined elsewhere

        fetchRequest.sortDescriptors = [sortDescriptor]
        fetchRequest.predicate = predicate

        return fetchRequest
    }

    func fetchData() {
        let fetchRequest = orderFetchRequest()
        fetchedResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath:nil, cacheName: nil)

        do {
            try fetchedResultController.performFetch()
        }
        catch let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
        }
    }
}

我只是复制粘贴了它。希望它能对你有用。如果不行,我会帮助你。


1
嗨,感谢您的回复,但上面的代码是用于获取两个日期之间的结果。但我的问题是如何按日期逐个获取结果。这意味着日期将成为部分,每个日期将有一组结果行。 - Chandan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接