Update core data object swift 3





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







10















I want to update a core data object in swift 3. After some googled I didn't found anything about swift 3.
So my question is: how can I update a core data object in swift 3?










share|improve this question























  • In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

    – mic
    Sep 13 '16 at 10:26




















10















I want to update a core data object in swift 3. After some googled I didn't found anything about swift 3.
So my question is: how can I update a core data object in swift 3?










share|improve this question























  • In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

    – mic
    Sep 13 '16 at 10:26
















10












10








10


5






I want to update a core data object in swift 3. After some googled I didn't found anything about swift 3.
So my question is: how can I update a core data object in swift 3?










share|improve this question














I want to update a core data object in swift 3. After some googled I didn't found anything about swift 3.
So my question is: how can I update a core data object in swift 3?







ios swift3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 3 '16 at 18:58









Roberto ToniniRoberto Tonini

97313




97313













  • In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

    – mic
    Sep 13 '16 at 10:26





















  • In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

    – mic
    Sep 13 '16 at 10:26



















In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

– mic
Sep 13 '16 at 10:26







In what ways do you think updating a managed object is different in Swift 3? Do you run into specific problems which didn't occur in Swift 2? If so, please share more details.

– mic
Sep 13 '16 at 10:26














2 Answers
2






active

oldest

votes


















21














Try this



        let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '(empId)'")
fetchRequest.predicate = predicate
do
{
let test = try context?.fetch(fetchRequest)
if test?.count == 1
{
let objectUpdate = test![0] as! NSManagedObject
objectUpdate.setValue("newName", forKey: "name")
objectUpdate.setValue("newDepartment", forKey: "department")
objectUpdate.setValue("001", forKey: "empID")
do{
try context?.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}





share|improve this answer





















  • 2





    Worked like a charm!

    – OHS
    Mar 19 '17 at 11:49






  • 1





    Thank you! It's worked well.

    – ssowri1
    Nov 1 '17 at 8:50











  • Thanks , but do you have to make empID unique?

    – NavodaP
    Feb 14 '18 at 3:48





















0














Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:



func context() ->  NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}



func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}

func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)

let result = try? context().fetch(fetchRequest)

if result?.count == 1 {

let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}





share|improve this answer


























  • Can you please add more detail about your solution to your answer, please?

    – CodeF0x
    Nov 22 '18 at 10:32











  • Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

    – Sandeep Kalia
    Nov 22 '18 at 10:38












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38751632%2fupdate-core-data-object-swift-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









21














Try this



        let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '(empId)'")
fetchRequest.predicate = predicate
do
{
let test = try context?.fetch(fetchRequest)
if test?.count == 1
{
let objectUpdate = test![0] as! NSManagedObject
objectUpdate.setValue("newName", forKey: "name")
objectUpdate.setValue("newDepartment", forKey: "department")
objectUpdate.setValue("001", forKey: "empID")
do{
try context?.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}





share|improve this answer





















  • 2





    Worked like a charm!

    – OHS
    Mar 19 '17 at 11:49






  • 1





    Thank you! It's worked well.

    – ssowri1
    Nov 1 '17 at 8:50











  • Thanks , but do you have to make empID unique?

    – NavodaP
    Feb 14 '18 at 3:48


















21














Try this



        let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '(empId)'")
fetchRequest.predicate = predicate
do
{
let test = try context?.fetch(fetchRequest)
if test?.count == 1
{
let objectUpdate = test![0] as! NSManagedObject
objectUpdate.setValue("newName", forKey: "name")
objectUpdate.setValue("newDepartment", forKey: "department")
objectUpdate.setValue("001", forKey: "empID")
do{
try context?.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}





share|improve this answer





















  • 2





    Worked like a charm!

    – OHS
    Mar 19 '17 at 11:49






  • 1





    Thank you! It's worked well.

    – ssowri1
    Nov 1 '17 at 8:50











  • Thanks , but do you have to make empID unique?

    – NavodaP
    Feb 14 '18 at 3:48
















21












21








21







Try this



        let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '(empId)'")
fetchRequest.predicate = predicate
do
{
let test = try context?.fetch(fetchRequest)
if test?.count == 1
{
let objectUpdate = test![0] as! NSManagedObject
objectUpdate.setValue("newName", forKey: "name")
objectUpdate.setValue("newDepartment", forKey: "department")
objectUpdate.setValue("001", forKey: "empID")
do{
try context?.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}





share|improve this answer















Try this



        let empId = "001"
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails")
let predicate = NSPredicate(format: "empId = '(empId)'")
fetchRequest.predicate = predicate
do
{
let test = try context?.fetch(fetchRequest)
if test?.count == 1
{
let objectUpdate = test![0] as! NSManagedObject
objectUpdate.setValue("newName", forKey: "name")
objectUpdate.setValue("newDepartment", forKey: "department")
objectUpdate.setValue("001", forKey: "empID")
do{
try context?.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 14 '18 at 13:07

























answered Dec 13 '16 at 6:31









RajeshKumar RRajeshKumar R

5,74812050




5,74812050








  • 2





    Worked like a charm!

    – OHS
    Mar 19 '17 at 11:49






  • 1





    Thank you! It's worked well.

    – ssowri1
    Nov 1 '17 at 8:50











  • Thanks , but do you have to make empID unique?

    – NavodaP
    Feb 14 '18 at 3:48
















  • 2





    Worked like a charm!

    – OHS
    Mar 19 '17 at 11:49






  • 1





    Thank you! It's worked well.

    – ssowri1
    Nov 1 '17 at 8:50











  • Thanks , but do you have to make empID unique?

    – NavodaP
    Feb 14 '18 at 3:48










2




2





Worked like a charm!

– OHS
Mar 19 '17 at 11:49





Worked like a charm!

– OHS
Mar 19 '17 at 11:49




1




1





Thank you! It's worked well.

– ssowri1
Nov 1 '17 at 8:50





Thank you! It's worked well.

– ssowri1
Nov 1 '17 at 8:50













Thanks , but do you have to make empID unique?

– NavodaP
Feb 14 '18 at 3:48







Thanks , but do you have to make empID unique?

– NavodaP
Feb 14 '18 at 3:48















0














Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:



func context() ->  NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}



func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}

func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)

let result = try? context().fetch(fetchRequest)

if result?.count == 1 {

let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}





share|improve this answer


























  • Can you please add more detail about your solution to your answer, please?

    – CodeF0x
    Nov 22 '18 at 10:32











  • Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

    – Sandeep Kalia
    Nov 22 '18 at 10:38
















0














Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:



func context() ->  NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}



func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}

func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)

let result = try? context().fetch(fetchRequest)

if result?.count == 1 {

let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}





share|improve this answer


























  • Can you please add more detail about your solution to your answer, please?

    – CodeF0x
    Nov 22 '18 at 10:32











  • Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

    – Sandeep Kalia
    Nov 22 '18 at 10:38














0












0








0







Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:



func context() ->  NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}



func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}

func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)

let result = try? context().fetch(fetchRequest)

if result?.count == 1 {

let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}





share|improve this answer















Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values:



func context() ->  NSManagedObjectContext {
let context=(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
return context
}



func save() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}

func UpdateCartByTestId(id:Int64,name:String) {
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Update")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.predicate = NSPredicate(format:"id == %d",id)

let result = try? context().fetch(fetchRequest)

if result?.count == 1 {

let dic = result![0]
dic.setValue(id, forKey: "id")
dic.setValue(name, forKey: "name")
save()
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 10:42









CodeF0x

2,00451021




2,00451021










answered Nov 22 '18 at 10:25









Sandeep KaliaSandeep Kalia

295




295













  • Can you please add more detail about your solution to your answer, please?

    – CodeF0x
    Nov 22 '18 at 10:32











  • Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

    – Sandeep Kalia
    Nov 22 '18 at 10:38



















  • Can you please add more detail about your solution to your answer, please?

    – CodeF0x
    Nov 22 '18 at 10:32











  • Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

    – Sandeep Kalia
    Nov 22 '18 at 10:38

















Can you please add more detail about your solution to your answer, please?

– CodeF0x
Nov 22 '18 at 10:32





Can you please add more detail about your solution to your answer, please?

– CodeF0x
Nov 22 '18 at 10:32













Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

– Sandeep Kalia
Nov 22 '18 at 10:38





Pass unique id in variable "id"(Unique variable created in Core data model) and all the variable as you want to update values.

– Sandeep Kalia
Nov 22 '18 at 10:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38751632%2fupdate-core-data-object-swift-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?