Am Rewriting my existing objective c code(ios) to swift and now am facing some issues with Coredata NSPredicate in swift as there is some many to one relationships and vice-versa.. am using the following code,
let fetchRequest = NSFetchRequest(entityName: "TUMessage")
let sortSequence = NSSortDescriptor(key: "sequence", ascending: true)
let sortTime = NSSortDescriptor(key: "epoch_time", ascending: true)
var filterPredicate:NSPredicate = NSPredicate(format:"thread == [c] %#",contact.threads.anyObject())
 fetchRequest.predicate = filterPredicate
var sortDescriptor:NSArray = NSArray(objects: sortTime,sortSequence)
fetchRequest.sortDescriptors = sortDescriptor
fetchRequest.fetchBatchSize = 50
return fetchRequest
where "TUMessage" is the table we need to fetch data from, "sequence" for sorting the fetched result, "thread == [c] %#" is a relationship of message table (many to one since each contact have multiple thread) ,
and contact.threads.anyObject() is the predicate which am trying to add.Unfortunately am getting the following error
type anyobject? does not confirm to protocol, CVarArgType
Any help would be appreciated..
Thank you
You need to unwrap it and cast to the specific class
contact.threads.anyObject()! as MYClass
Related
I would like to move the index 0 to end of array using NSPredicate.
I am using Core Data and I having an entity called Link that is have an attribute called orderingLink which is Integer 64 type.
I've tried to use this follow code and i've expected to move the index 0 to end of array.
open func fetchLinkRequest(_ links: [Links]? = nil) -> NSFetchRequest<NSFetchRequestResult> {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: T.entityName)
let sort1 = NSSortDescriptor(key: "orderingLink", ascending: true)
fetchRequest.sortDescriptors = [sort1]
var predicates = [NSPredicate]()
predicates.append(NSPredicate(format: "orderingLink != 0 ", argumentArray: [links]))
fetchRequest.predicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.and, subpredicates: predicates)
return fetchRequest
Although, this code don't show the index 0 in array, and this was my latest progress in order to move the index 0 to end of array.
Please, help me how to solve this problem.
Thank You.
The answer to this is "You can't." Predicates let you sort or filter arrays or databases, but not modify them.
I have entity NSNote which is having one-to-one relationship with NSContent
The name of the relationship is content.
I notice the covention content.[some properties] workable for sortDescriptors, and not propertiesToUpdate/ propertiesToFetch
Supported in sortDescriptors
The following is a workable code
let liteBodySortingSortDescriptor = NSSortDescriptor(key: "content.liteBodySorting", ascending: true, selector: #selector(NSString.caseInsensitiveCompare))
let sortDescriptors = [liteBodySortingSortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors
Not workable in propertiesToUpdate
let batchUpdateRequest = NSBatchUpdateRequest(entityName: "NSPlainNote")
batchUpdateRequest.predicate = NSPredicate(format: "self = %#", item.objectID)
batchUpdateRequest.propertiesToUpdate = ["content.body" : updateBody.body as Any]
batchUpdateRequest.resultType = .updatedObjectIDsResultType
// reason: 'Invalid string keypath content.body passed to propertiesToUpdate:'
Not workable in propertiesToFetch
let fetchRequest = NSFetchRequest<NSPlainNote>(entityName: "NSPlainNote")
fetchRequest.predicate = NSPredicate(format: "self = %#", objectID)
fetchRequest.propertiesToFetch = [
"kindValue",
"content.body",
"content.searchedString",
"liteBody",
"content.liteBodySorting",
"locked",
]
// reason: 'Bad fetch request (NSManagedObjectResultType not compatible with contents of propertiesToFetch)'
I was wondering, is my findings correct? If it is so, how I should make it work for propertiesToUpdate and propertiesToFetch?
This question already has answers here:
#FetchRequest + case-insensitive sorting - SwiftUI & CoreData
(1 answer)
NSSortDescriptor in Swift
(7 answers)
Closed 1 year ago.
I want to sort items in sequence order as you can see in screenshot attached. To sort by last word in its name. Eg. Part 1, Part 2,...etc.
let request : NSFetchRequest<DisplayHelper> = DisplayHelper.fetchRequest()
print("type set is : \(type)")
let predicate = NSPredicate(format: "type == %#", type)
request.predicate = predicate
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let sortDescriptors = [sortDescriptor]
request.sortDescriptors = sortDescriptors
I am trying to create an NSFetchRequest to fetch the most recent 80 items from my entity and sort them in ascending order.
Is it possible to achieve this in one request? Currently I am doing two requests. One to get the date of the most recent item offset by 80 and then using that date in the second request.
What I want to achieve is:
let request:NSFetchRequest<Item> = Item.fetchRequest()
let sort = NSSortDescriptor(key: "itemTimestamp", ascending: false)
request.sortDescriptors = [sort]
request.predicate = NSPredicate(format: "itemGroupId == %#", self.itemGroupId)
request.fetchLimit = 80
// add logic to sort the returned results in ascending: true
I want to fetch object from Core Data using NSPredicate. I made it, but when I launch NSFetchRequest the NSPredicate equals nil shown in the console. I have the Xcode 6.4, Swift 1.2. I restarted Xcode but I get the same result. How can I make it?
func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Car")
let sortDescriptor = NSSortDescriptor(key: "personCar", ascending: true)
fetchRequest.predicate = NSPredicate(format: "personRelationship.namePerson contains[c] %#", currentPerson)
fetchRequest.fetchBatchSize = 50
fetchRequest.sortDescriptors = [sortDescriptor]
println(fetchRequest)
return fetchRequest
}
The message from the console
<NSFetchRequest: 0x1700db040> (entity: Car; predicate: ((null)); sortDescriptors: ((
"(personCar, ascending, compare:)"
)); batch size: 50; type: NSManagedObjectResultType; )
Make sure that your fetchRequest.predicate value is set (i.e., is not nil).
The code in your post does show that the predicate value is assigned. However, when the NSFetchRequest.predicate attribute is not set then the println console message will display ((null)) for the predicate value.