比较 Fetchrequest Xcode Swift OSX 核心数据的谓词中的 NSSet
Posted
技术标签:
【中文标题】比较 Fetchrequest Xcode Swift OSX 核心数据的谓词中的 NSSet【英文标题】:compare NSSet in Predicate for Fetchrequest Xcode Swift OSX Core Data 【发布时间】:2016-08-11 09:26:02 【问题描述】:我有两个NSManagedObject
喜欢
class Person: NSManagedObject
@NSManaged var firstname: String;
@NSManaged var lastname: String;
@NSManaged var code: String;
@NSManaged var position: Int16;
@NSManaged var lasttime: NSDate;
@NSManaged var isvip: Int16;
@NSManaged var changeposition: NSSet;
class ChangePosition: NSManagedObject
@NSManaged var person:Person
@NSManaged var code: String;
@NSManaged var from: Int16;
@NSManaged var to: Int16;
@NSManaged var time: NSDate;
所以我有一个人与许多ChangePosition
作为NSSet
。每个ChangePosition
都有一个NSDate
。
现在我想尝试从NSDate
最高的人那里获取ChangePosition
。我可以为此使用哪个谓词结构?
【问题讨论】:
【参考方案1】:所以我的实际工作流程就是这个
let fetchReqPerson = NSFetchRequest(entityName: "Person");
//fetchReq.predicate = NSPredicate(format: "ANY personchangeposition.time == maxElement(personchangeposition.time)")
do
let fetchResult = try self.appDelegate.managedObjectContext.executeFetchRequest(fetchReqPerson)
for managedObject in fetchResult
var bigTime:PersonChangePosition?
for changes in (managedObject as! Person).personchangeposition
if (changes as! PersonChangePosition).time.compare(_date) == NSComparisonResult.OrderedAscending
if bigTime == nil
bigTime = changes as! PersonChangePosition
else
if (changes as! PersonChangePosition).time.compare(bigTime!.time) == NSComparisonResult.OrderedDescending
bigTime = changes as! PersonChangePosition;
if bigTime != nil
//print("bigTime: ",bigTime!.time, "to: ",bigTime!.to)
self.fetchResultPerson.append(bigTime!)
self.fetchForRecord = self.fetchResultPerson;
//print("fetchResult fetchResultPerson: ",fetchResultPerson.count)
catch let fetchError as NSError
print("Fetching error: ",fetchError)
//
switch(self.areaSegementControl.selectedSegment)
case 0: self.fetchForRecord = try self.fetchForRecord.filter($0.to != 4)
break;
case 1: self.fetchForRecord = try self.fetchForRecord.filter($0.to == 1 || $0.to == 3)
break;
case 2: self.fetchForRecord = try self.fetchForRecord.filter($0.to == 2)
break;
default:
break;
当然,它相当慢......不是很好
【讨论】:
以上是关于比较 Fetchrequest Xcode Swift OSX 核心数据的谓词中的 NSSet的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 6.1 中的 NSCompoundPredicate 错误