我现在正在调试以下代码几个小时。
对我来说,看起来我的NSPredicate被忽略了。
我在核心数据库中有2个条目。无论我设定什么谓词,我总是回到这两个条目。两者都有一个名为type
的属性为1.即使我将谓词设置为(type == 5)
,也会获取相同的2个条目。
// MARK: - Fetched results controller
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: self.managedObjectContext!)
fetchRequest.entity = entity
// simply ignores verything here...
// -----------------------------------------------
// I can use (type == 5) whereby the db only contains items with type == 1...
// It's like the predicate is nevery used in the fetchrequest.
fetchRequest.predicate = NSPredicate(format: "(name LIKE '%Eier%')")!
println(fetchRequest.predicate)
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Main")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
abort()
}
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil