Fetch the objects from Coredata and return it as NSMutableArray iOS Swift

Solution:

If you have to fetch the array of objects form coredata and display it in a tableView then don't worry here is the example.

The below example had the fetch the objects from coredata orderedobjects also it had a condition with except the particular value fetch all the object predicate also there.Finally return the fetched objects using predicate from coredata. 

func getallData () -> NSMutableArray{
    let myArr: NSMutableArray = NSMutableArray()
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    //create a fetch request, telling it about the entity
    let fetchRequest: NSFetchRequest<ENTITY> = ENTITY.fetchRequest()
    
        fetchRequest.predicate = NSPredicate(format: "NOT (attribute == value)")
    do {
        let searchResults = try appDelegate.getContext().fetch(fetchRequest)
        for Entity in searchResults as [NSManagedObject] {
            myArr.add(Entity)
        }
    } catch {
        print("Error with request: \(error)")
    }
    
    return myArr

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift