How to update managed object data? - ios

I have started my first core data application. I am working with one entity right now called 'Folder'.
The first view controller displays all the Folders in a tableview, which I can add to and it reloads the data. This works fine because It uses the fetch request to populate the table.
override func viewWillAppear(animated: Bool) {
var error: NSError?
let request = NSFetchRequest(entityName: "Folder")
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
self.events = moc?.executeFetchRequest(request, error: &error) as! [Folder]
self.UITable.reloadData()
}
However when segueing to another view controller via the table cell I pass on the selected Folder data to the controller using the index path. e.g.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showDetails" {
let destinationVC = segue.destinationViewController as! FolderDetailsViewController
let indexPath = UITable.indexPathForSelectedRow()
let selectedFolder = folders[indexPath!.row]
destinationVC.selectedFolder = selectedFolder
}
}
My second view controller uses the data passed from the first table view to display in textfields:
var selectedFolder: Folder!
folderNameLabel.text = selectedFolder?.title
folderDetailsLabel.text = selectedFolder?.details
folderDateLabel.text = displayDate
I then have a modal to edit/save the folder data in a modal appearing from the second controller:
//Edit and save event
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
//Error
var error: NSError?
//Storing Data from fields
SelectedFolder!.title = FolderName.text
SelectedFolder!.details = FolderDetails.text
SelectedFolder!.date = FolderDate.date
context?.save(&error)
self.dismissViewControllerAnimated(true, completion: {});
When dismissing the modulate data is not updated, I have to go back to the first controller to reload the data and segue again.
I think this is because I have no NSFetchRequest (or NSFetchResultsController) to get the most recent changes.
What is the best method to reload the data of the selectedFolder when I make the changes in the modal ?

You can refresh your second view in viewWillAppera() if your modal view is presented in full screen.
override func viewWillAppear(animated: Bool) {
{
folderNameLabel.text = selectedFolder?.title
folderDetailsLabel.text = selectedFolder?.details
folderDateLabel.text = displayDate
}

It seems like you would want to call moc.refreshObject(folder, mergeChanges:true)
See the documentation here.

Related

Transfer data using prepare for segue

I'm new to swift and firebase services,
I'm using fire store data base as my database and I have a first table view that reads all the data and put it in a nice tableview. every document in my table view has a sub collection. when a user press a row I want it to open a second table view with the sub collection.
this is my prepare for segue code :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableViewDishes.indexPathForSelectedRow {
db.collection("Restaurants").document("Applebees").collection("Menu").document(sections[indexPath.section].sectionName!).collection("Dishes").document(sections[indexPath.section].listofDishes![indexPath.row].DishName).collection("Options").getDocuments { (querySnapshot, error) in
if error != nil {print(error)}
else {
for document in querySnapshot!.documents {
//adding all the data to an array called myOption
}
}
}
let selectedDishTableViewController = segue.destination as! SelectedDishViewController
selectedDishTableViewController.myOption = self.myOption
selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row]
selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName!
self.myOption.removeAll()
}
}
the issue is that once my code reach the db.collection line it jumps right away to after the for loop when myOption is a empty array and only then it comes back and appending objects to my array.
that cause the first time I press a row get an empty second table view and when I go back and press it again I get the required information.
db.collection() does some async work, so all this code:
let selectedDishTableViewController = segue.destination as! SelectedDishViewController
selectedDishTableViewController.myOption = self.myOption
selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row]
selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName!
self.myOption.removeAll()
should be right before the for loop where you set myOption array. That way, everything will be set up once the database has retrieved all the data and you have made all the setup inside the for loop.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableViewDishes.indexPathForSelectedRow {
db.collection("Restaurants").document("Applebees").collection("Menu").document(sections[indexPath.section].sectionName!).collection("Dishes").document(sections[indexPath.section].listofDishes![indexPath.row].DishName).collection("Options").getDocuments { (querySnapshot, error) in
if error != nil {print(error)}
else {
for document in querySnapshot!.documents {
//adding all the data to an array called myOption
}
let selectedDishTableViewController = segue.destination as! SelectedDishViewController
selectedDishTableViewController.myOption = self.myOption
selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row]
selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName!
self.myOption.removeAll()
}
}
}
}
Since its an Async call you might have to write the selectedDishTableViewController segue in the callback.
Recommendation : Seque delegate method should be simple and should not be handling all these db operation try to optimise it.

Core data object get saved, but doesn't show up in table until I pop to related object table and then come forward

I have three view controllers embedded in a navigation controller.
The first view controller is a list of tests, which use fetchedResultsController to populate the table.
The second view controller is a detail view controller, which allows adding a new test, editing it's detail, saving the new test or adding Questions to the test. It also includes a table of questions, which is also populated with a fetchedResultsController.
The problem I have is that when I have an existing Test which is saved, then when I go to add on questions, they correctly populate the table when I pop back to the TestDetailsVC, however, if I'm adding a new test, and then adding new questions to it, the test saves to core data, then in the QuestionDetailsVC questions also save to core data. However, when I pop back to the TestDetailsVC, the new questions don't populate the table. However, when I navigate back to the TestListVC and then go forward, the questions then populate the table correctly. I'm trying to get the questions to populate the table the first time I pop back to the table. Shouldn't the table re-fetche this data when the view loads, and the table work correctly when I navigate back since these objects are saved in core data? Why does it work correctly with a previously saved test object, but not work with a newly saved test object?
My code for saving the new test in the TestDetailsVC is:
//if we tap on a row, then we select that question to edit
//if a question is selected, we will pass that information over to
//the question editor view so it can be edited
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//make sure there is at least one question
//objs = means object selected
if let objs = controller.fetchedObjects, objs.count > 0 {
//if there is, keep track of the test which is selected
let question = objs[indexPath.row]
//pass along that test to the editor to be edited
//the sender is the selected test at that particular row
performSegue(withIdentifier: "EditQuestionSegue", sender: question)
}
}
//we need to get ready to do the segue before we call it
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
switch identifier {
case "EditQuestionSegue":
//find the question needing to be edited
//pass this along to the question editing view controller
if let destination = segue.destination as? QuestionDetailsVC {
if let question = sender as? Question {
destination.questionToEdit = question
//pass along the test that the question is
//related to also
if let test = testToEdit {
destination.testToEdit = test
}
}
}
case "AddNewQuestionSegue":
//if no test to edit got passed in, it means
//we're now editing a new test
//once we type in the test title, then we have to
//make sure the test has been saved if it's new test
//before we go on to edit questions
if testToEdit == nil {
saveTestBeforeAddingQuestion()
//make sure we set up as if new
//before we start adding questions
//these are called in the view did load
}
if let destination = segue.destination as? QuestionDetailsVC {
if let test = testToEdit {
destination.testToEdit = test
}
}
default:
print("no segue this time")
}
}
}
func saveTestBeforeAddingQuestion() {
var test: Test!
//if there's not a passed in value into the testToEdit core data
//object test entity, then we're going to edit as if new
if testToEdit == nil {
//then instantiate a new test object ready to be written to
test = Test(context: context)
} else {
test = testToEdit
}
if let title = titleTextField.text {
test.title = title
}
if let abrevTitle = abrevTitleTextField.text {
test.abrevTitle = abrevTitle
}
if let author = authorTextField {
test.author = author.text
}
if let publisher = publisherTextField {
test.publisher = publisher.text
}
ad.saveContext()
//since we have now saved a new test
//let's put that test into our testToEdit variable
//so we can pass it along to the next view controller
//during our segue
if let newTestCreated = test {
testToEdit = newTestCreated
}
}
My code for saving the new question is:
#IBAction func savePressed(_ sender: UIButton) {
var question: Question!
//if there's not a passed in value into the questionToEdit core data
//object test entity, then we're going to edit as if new
if questionToEdit == nil {
//then instantiate a new test object ready to be written to
question = Question(context: context)
} else {
question = questionToEdit
}
//make sure to relate the question added to the testToEdit test
question.test = self.testToEdit
//if there is something in the sentence text field
//then assign that value to the question.sentence attribute
//of the sentence entity in our core data context
if let sentence = questionSentenceTextField.text {
question.sentence = sentence
}
if let identifier = questionIdentifierTextField.text {
question.identifier = identifier
}
if let displayOrder = questionDisplayOrderTextField.text {
question.displayOrder = Int(displayOrder) as NSNumber?
}
// save the context
ad.saveContext()
_ = navigationController?.popViewController(animated: true)
}
I spent days trying to get the question table to load, but finally discovered that it wasn't loading back up because when I pop back to the view controller it doesn't automatically reload the table with the fetchedResultsController. So I had to place a call to refetch the data in the viewWillAppear method and then reload the tableView:
(TestDetailsVC)
override func viewWillAppear(_ animated: Bool) {
guard testToEdit != nil else {return}
attempFetch()
tableView.reloadData()
}

Passing data between view controllers through segue

I have a MapViewController with a prepareForSegue(_:sender:)method, which I intend to use to send data to LandmarkTableViewController, and is called when a button is pressed.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if let identifier = segue.identifier {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
switch identifier {
case "showLibrary" : landmarkvc.passedLandmark = library // pass data to LandmarkTableViewController
case "showBank" : landmarkvc.passedLandmark = bank // pass data to LandmarkTableViewController
default : break
}
}
}
}
The LandmarkTableViewController is properly set up to display the String array properties, with one String on each row. So what I intend to do is pass the appropriate data for the table to properties according to which button was pressed, and let LandmarkTableViewController display the corresponding properties.
class LandmarkTableViewController: UITableViewController {
var properties = [String]()
var passedLandmark = Landmark(name: "temp", properties: ["temp"]) // initially set to default value
override func viewDidLoad() {
super.viewDidLoad()
loadSampleProperties()
}
func loadSampleProperties() {
self.properties = passedLandmark!.properties
}
// other methods....
}
class Landmark {
var name: String
var properties: [String]
init?(name: String, properties: [String]) {
self.name = name
self.properties = properties
// Initialization should fail if there is no name or if there is no property.
if name.isEmpty || properties.isEmpty {
return nil
}
}
However, when I run the code, only temp is displayed in the table view. I've been stuck on this for a long time now, so any help is much appreciated!
Edit: loadData() inside of viewDidLoad() is changed to the correct loadSampleProperties(). I made an error while posting the code to the question.
I think this should solve your problem if not double check your identifiers
and you can make sure to data passing with adding print(passedLandmark) to viewDidLoad() or breakpoint to make sure you getting the data
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if segue.identifier == "showLibrary" {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
landmarkvc.passedLandmark = library
}
if segue.identifier == "showBank" {
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
landmarkvc.passedLandmark = bank
}
}
}
Hope this will helps
Code is missing from your quote, so I can't be sure, but I assume your loadData() method is the one that reloads the table view data with Landmark you've passed in prepareForSegue. If that is the case:
viewDidLoad() is called before prepareForSegue, so that all the views and elements of the destinationViewController are loaded and ready to use. Thus, in your case, the table view is loaded with your "temp" data and nothing makes it reload when you set the proper one.
You have two options:
You could call loadData()/reloadData() in viewWillAppear for example, which is called after prepareForSegue(). Bare in mind that viewWillAppear will possibly be called again in some other navigation.
Otherwise, you could instantiate and present/push the new controller in your parent view controller, instead of using the segue.

Passing data between two view controllers are not working

I am trying to pass some data between two view controllers, but it doesn't work..
This is the data i am trying to pass(these has items from parse.com - the same code is in both view controllers):
var userFile = [PFFile]()
var createdAt = [NSDate]()
var objID = [String]()
This is the button for open the view controller(inside the first view controller i am trying to send data FROM):
#IBAction func openButtonAction(sender: AnyObject) {
let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
modalVC.userFile = self.userFile
modalVC.createdAt = self.createdAt
modalVC.objID = self.objID
print("USERFILE: \(modalVC.userFile.count)")
presentViewController(modalVC, animated: true, completion: nil)
}
The view controller is a ModalViewController.xib connected to ViewStoryModalViewController.swift
This is the viewDidLoad in the view controller i am trying to send data TO:
override func viewDidLoad() {
super.viewDidLoad()
print("USERFILECOUNT: \(self.userFile.count)")
}
My problem is that this is the messages i get in xCode output:
What might be wrong here? Any suggestions?
xCode output tells that an array self.userFile contains zero elements, It doesn't mean that it is passed wrong. It is just empty.
print("USERFILECOUNT: \(self.userFile.count)")
Check if it is empty before passing it to modal vc.
Try this code
You first need to present after that try to set variable.
IBAction func openButtonAction(sender: AnyObject) {
let modalVC = ModalViewController(nibName: "ModalViewController", bundle: nil)
print("USERFILE: \(modalVC.userFile.count)")
presentViewController(modalVC, animated: true, completion: nil)
modalVC.userFile = self.userFile
modalVC.createdAt = self.createdAt
modalVC.objID = self.objID
}

Passing VC data not working properly

When a user taps on an image in my application, I would like for that to send them to another view controller to give them more post details but I can't quite figure out how to transfer that image from one view controller to the next!
The code below, should they tap the image, should send them to the detailed view controller - this is done so in the else if statement.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if username != PFUser.currentUser()?.username {
let profileVC: UserProfileViewController = segue.destinationViewController as! UserProfileViewController
profileVC.usernameString = username
} else if (segue.identifier == "toPostDetail") {
var svc = segue.destinationViewController as! PostDetailsViewController
svc.toPass = // Call/pass the image
}
}
To retrieve all the posts in my database I use the following code, this runs perfectly fine but I need to get the postImage trasnfered to the next view controller.
postsArray[indexPath.row]["image"].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
postCellObj.postImage.image = downloadedImage
postCellObj.postImage.layer.masksToBounds = true
postCellObj.postImage.layer.cornerRadius = 5.0
}
}
So what would I put in the svc.toPass in order for the image to be sent to the other view controller? Since this is an array of posts, would I have to do something with didSelectRowAtIndexPath or am I doing the right thing? I am also using Parse.com to get information to and from my databases.
You should pass the post rather than the image, so it would be something like
postsArray[indexPath.row]
How you get the indexPath is up to you. If the segue is triggered by a cell selection then you could store the indexPath in didSelectRowAtIndexPath, or you could pass the indexPath as the sender when you trigger the segue, or you could use indexPathForSelectedRow if it's retained during the segue process.

Resources