Coredata get the entity as message based on get message by id iOS swift

 Solution:

func getMessageByID(messageID: String) -> Message?

{

    var wgs: [Message] = []

    let wgFetchRequest: NSFetchRequest<Message> = Message.fetchRequest()

    wgFetchRequest.returnsObjectsAsFaults = false

    let predicate = NSPredicate(format: "message_id == %@",messageID)

    wgFetchRequest.predicate = predicate

    do {

        let objects = try  getContext().fetch(wgFetchRequest)

        

        wgs.append(contentsOf: objects)

    } catch {

        customPrint(val:"Error with request: \(error)")

    }

    return wgs.count>0 ? wgs[0] : nil

}


//  MARK: -  Get the context

func getContext () -> NSManagedObjectContext {

    var context: NSManagedObjectContext?

    if #available(iOS 10.0, *) {

        context = coredata.persistentContainer.viewContext

    } else {

        context = coredata.managedObjectContext

    }

    return context!

}


In above function coredata as the context that initialised in your appdelegate.

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift