Executefetchrequest Doesn't Return the Nsmanagedobject Subclass

executeFetchRequest doesn't return the NSManagedObject subclass

Just figured out the solution.

I had to add the @objc attribute to allow the class to be compatible with Objective-C.

Now the fetch request is returning a correct result of Tasks[]

import Foundation
import CoreData

@objc(Task) // make compatible with objective-c
class Task : NSManagedObject
{
@NSManaged var note: String!
@NSManaged var completed: Bool
}

Reference: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_36

NSManagedObject subclass' category equivalent in Swift?

A Swift extension is the right way to define additional methods for NSManagedObject subclasses.
You can put the extension into a separate file
(e.g. "BookExtensions.swift") so that they are not overwritten when
"Book.swift" is recreated in Xcode.

How to get extension to return subclass type

You just have to make this generic!

extension NSManagedObject {

func inContext<T: NSManagedObject>(_ context: NSManagedObjectContext) -> T? {
let objectID = self.objectID
let childObject = context.object(with: objectID)
return childObject as? T
}
}

Unfortunately you have to call it like this, with a type annotation, unless the type can be inferred.

let objectInContext: MyManagedobjectType? = myManagedObject.inContext(someContext, type: MyManagedObjectType.self)

Core data fetchRequest gives executeFetchRequest:error: null is not a valid NSFetchRequest

Edit

I got It working when I simplified fetchRequest's declaration. Instead of using:

let allItemFetchRequest: NSFetchRequest<Item>
if #available(iOS 10.0, *) {
allItemFetchRequest = Item.fetchRequest() as! NSFetchRequest<Item>
} else {
// Fallback on earlier versions
allItemFetchRequest = NSFetchRequest(entityName:"Item")
}

Try using:

let entityName = String(describing: Item.self)
let request = NSFetchRequest<Item>(entityName: entityName)

Cheers!

SwiftUI @FetchRequest crashes the app and returns error

In the @enviromentVAr, sometimes, you need to set the enviroment by yourself.

 let managedObjectContext: NSManagedObjectContext =  ((UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!

ContentView().environment(\.managedObjectContext, managedObjectContext)

Then the managedObjectContext can work.

 @Environment(\.managedObjectContext) var managedObjectContext

Core Data - NSManagedObject returning nil

The issue is that AppDelegate() creates always a new instance of the class which is not the same as the "hard-coded" delegate instance of the application.

You have to write

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let fetchedRecords = appDelegate.fetchRecords(fromEntity: "Dashboard")
appDelegate.displayRecords(fromFetchedRecords: fetchedRecords)

Since you have created a custom subclass of NSManagedObject use it as type rather than NSManagedObject or – worse – the unspecified AnyObject. It makes many things much easier:

func fetchRecords(fromEntity entity:String) -> [Dashboard] {
let fetchRequest = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)!
fetchRequest.entity = entityDescription
do {
return try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [Dashboard]
} catch let fetchError as NSError {
print(fetchError!)
}
return [Dashboard]()
}

func displayRecords(fromFetchedRecords result:[Dashboard]) {
print("Total records:\(result.count)")

for dashboard in result { // the check for empty is not needed
print("Value: \(dashboard.count)")
}
}


Related Topics



Leave a reply



Submit