I am new to Core Data and I have encountered a problem. This app is supposed to store images from the photo library to core data and display those in a collection view.
However, the problem is the pictures display when the app is newly installed on the simulator and you just add pictures. But when you close the app and open again it crashes and shows an error in the console: Thread 1 EXC_BAD_ACCESS
// Loading Setup
#IBOutlet var collection: UICollectionView!
var images = [NSManagedObject]()
override func viewDidAppear(animated: Bool) {
let managedContext = AppDelegate().managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "FullRes")
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
images = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
collection.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return images.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MainCollectionViewCell
let selectedObject = images[indexPath.row]
print(selectedObject)
let image: UIImage = UIImage(data: selectedObject.valueForKey("imageData") as! NSData!)!
cell.imageView.image = image
// Configure the cell
return cell
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
}
*/
// Camera Setup
// the image picker
let imagePicker = UIImagePickerController()
// a queue to save the image without freezing the App UI
let saveQueue = dispatch_queue_create("saveQueue", DISPATCH_QUEUE_CONCURRENT)
// the Managed Object Context where we will save our image to.
let managedContext = AppDelegate().managedObjectContext
#IBAction func imageDidPress(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func addDidPress(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
prepareImageForSaving(image)
self.dismissViewControllerAnimated(true, completion: nil)
}
/*
func deletePhotoFromLibrary(info: [String : AnyObject]) {
print("wasrun")
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageUrls = [imageUrl]
//Delete asset
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs(imageUrls, options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
},
completionHandler: { success, error in
NSLog("Finished deleting asset. %#", (success ? "Success" : error!))
})
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}
*/
func prepareImageForSaving(image:UIImage) {
// use date as unique id
let date : Double = NSDate().timeIntervalSince1970
// dispatch with gcd.
dispatch_async(saveQueue) {
// create NSData from UIImage
guard let imageData = UIImageJPEGRepresentation(image, 1) else {
// handle failed conversion
print("jpg error")
return
}
// scale image
let thumbnail = self.scale(image: image, toSize: self.view.frame.size)
guard let thumbnailData = UIImageJPEGRepresentation(thumbnail, 0.7) else {
// handle failed conversion
print("jpg error")
return
}
// send to save function
self.saveImage(imageData, thumbnailData: thumbnailData, date: date)
}
}
func saveImage(imageData:NSData, thumbnailData:NSData, date: Double) {
dispatch_barrier_async(saveQueue) {
// create new objects in moc
guard let fullRes = NSEntityDescription.insertNewObjectForEntityForName("FullRes", inManagedObjectContext: self.managedContext) as? FullRes, let thumbnail = NSEntityDescription.insertNewObjectForEntityForName("Thumbnail", inManagedObjectContext: self.managedContext) as? Thumbnail else {
// handle failed new object in moc
print("moc error")
return
}
//set image data of fullres
fullRes.imageData = imageData
//set image data of thumbnail
thumbnail.imageData = thumbnailData
thumbnail.id = date as NSNumber
//set relationship between the two objects
thumbnail.fullRes = fullRes
// save the new objects
do {
try self.managedContext.save()
} catch {
// implement error handling here
fatalError("Failure to save context: \(error)")
}
// clear the moc
self.managedContext.refreshAllObjects()
}
}
func scale(image image:UIImage, toSize newSize:CGSize) -> UIImage {
// make sure the new size has the correct aspect ratio
let aspectFill = resizeFill(image.size, toSize: newSize)
UIGraphicsBeginImageContextWithOptions(aspectFill, false, 0.0);
image.drawInRect(CGRectMake(0, 0, aspectFill.width, aspectFill.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
func resizeFill(fromSize: CGSize, toSize: CGSize) -> CGSize {
let aspectOne = fromSize.height / fromSize.width
let aspectTwo = toSize.height / toSize.width
let scale : CGFloat
if aspectOne < aspectTwo {
scale = fromSize.height / toSize.height
} else {
scale = fromSize.width / toSize.width
}
let newHeight = fromSize.height / scale
let newWidth = fromSize.width / scale
return CGSize(width: newWidth, height: newHeight)
}
Update: Based on the comments made below, I have made changes to the code.
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MainCollectionViewCell
if let imagePath = images[indexPath.row].imageData {
cell.imageView.image = UIImage(data: imagePath)
}
// Configure the cell
return cell
}
But now, I get another error: 2015-12-26 20:10:24.464 Collect[480:69113] -[Thumbnail imageData]: unrecognized selector sent to instance 0x13fe9ba00.
The code class AppDelegate: UIResponder, UIApplicationDelegate { on AppDelegate.swift is highlighted.
It is an interesting thing that the app works until a certain point. For instance, when I uninstall the app from the phone I am running it in and then rerun it again, it works for a while until it crashes.
But still, it does not work perfectly like I hope it would be. Sometimes, one picture will appear only if another picture is added.
Update 2: I made some adjustments based on your comments below:
override func viewDidAppear(animated: Bool) {
authenticateUser()
let managedContext = AppDelegate().managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Thumbnail")
managedContext.performBlockAndWait { () -> Void in
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
self.images = results as! [Thumbnail]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
collection.reloadData()
}
It works fine whenever I delete the app and rerun it. However, when the app is closed, it no longer has the capability to restore the images.
I still get -[Thumbnail imageData]: unrecognized selector sent to instance 0x13762d650 while running it. The highlighted code is class AppDelegate: UIResponder, UIApplicationDelegate { on AppDelegate.swift
You are right, there is a mistake in my answer on the other question.
CoreData needs all operation in one NSManagedObjectContext to happen on the same thread as the one on which it was created. I updated the answer.
Here is the relevant code :
Create two separate queue's in the UIViewController
let convertQueue = dispatch_queue_create("convertQueue", DISPATCH_QUEUE_CONCURRENT) // this one is for image stuff
let saveQueue = dispatch_queue_create("saveQueue", DISPATCH_QUEUE_CONCURRENT) // this one is for CoreData
Set value of moc on the correct thread
extension ViewController {
// call this in viewDidLoad
func coreDataSetup() {
dispatch_sync(saveQueue) {
self.managedContext = AppDelegate().managedObjectContext
}
}
}
Save images with :
extension ViewController {
func saveImage(imageData:NSData, thumbnailData:NSData, date: Double) {
dispatch_barrier_sync(saveQueue) {
// create new objects in moc
guard let moc = self.managedContext else {
return
}
guard let fullRes = NSEntityDescription.insertNewObjectForEntityForName("FullRes", inManagedObjectContext: moc) as? FullRes, let thumbnail = NSEntityDescription.insertNewObjectForEntityForName("Thumbnail", inManagedObjectContext: moc) as? Thumbnail else {
// handle failed new object in moc
print("moc error")
return
}
//set image data of fullres
fullRes.imageData = imageData
//set image data of thumbnail
thumbnail.imageData = thumbnailData
thumbnail.id = date as NSNumber
thumbnail.fullRes = fullRes
// save the new objects
do {
try moc.save()
} catch {
fatalError("Failure to save context: \(error)")
}
// clear the moc
moc.refreshAllObjects()
}
}
}
Related
I have a tableView with multiple sections and i want to show in a cell (via notification) the progress of a download that is being handle by Alamofire.
Right now, i already have the notification post working and passing as info, an episode object, like this:
let info = ["episode": episode, "progress": progress.fractionCompleted] as [String : Any]
NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
Each of cells have an episode object. So i want to find the IndexPath of a cell that have an episode object that matches with the episode object that is being passed from a notification.
I can't figure out how can loop through my cells to find which one have that episode and get it's indexPath so i can respond to the notification properly.
I tried to get the index of the array that is being dataSource but as the tableView has multiple sections, this is not working.
Can someone help me? Thanks
My TableViewController:
//
// EpisodesViewController.swift
// Podee
//
// Created by Vinícius Barcelos on 21/07/18.
// Copyright © 2018 Vinícius Barcelos. All rights reserved.
//
import UIKit
import RealmSwift
import Kingfisher
class EpisodesTableViewController: UITableViewController {
//MARK:- Variables
var episodes: Results<Episode> = RealmService.shared.read(object: Episode.self).sorted(byKeyPath: "pubDate", ascending: true)
let episodesCellId = "episodesCellId"
var notificationToken: NotificationToken?
var episodesDictionary = Dictionary<Date, [Episode]>()
var dateDays = [Date]()
//MARK:- Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
setupObservers()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
deinit {
self.notificationToken?.invalidate()
//NotificationCenter.default.removeObserver(self, name: NSNotification.Name.downloadProgress, object: nil)
}
//MARK:- Setup
fileprivate func setupObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(handleDownloadProgressNotification(notification:)), name: .downloadProgress, object: nil)
}
}
#objc func handleDownloadProgressNotification(notification:Notification) {
////////
}
//MARK:- Tableview methods
override func numberOfSections(in tableView: UITableView) -> Int {
return episodesDictionary.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let key = dateDays[section]
guard let datesValues = episodesDictionary[key] else {
return 0
}
return datesValues.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMMM"
return dateFormatter.string(from: dateDays[section])
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: episodesCellId, for: indexPath) as! EpisodesTableViewCell
let key = dateDays[indexPath.section]
if let podcastValues = episodesDictionary[key] {
cell.delegate = self
cell.progressBar.isHidden = true
cell.episode = podcastValues[indexPath.row]
}
return cell
}
}
Download code:
// Start download
Alamofire.request(episode.streamURL).downloadProgress { (progress) in
// Send a notification about the download progress
let info = ["episode": episode, "progress": progress.fractionCompleted] as [String : Any]
NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
//print(progress)
// Check data
}.responseData { (responseData) in ......
Modify your function of download and add the following parameters
func downloadFile(url: String,date: Date, index: Int){
let utilityQueue = DispatchQueue.global(qos: .utility)
Alamofire.download(url)
.downloadProgress(queue: utilityQueue) { progress in
let info: [String: AnyHashable] = ["date": date,
"index" : index,
"progress": progress.fractionCompleted
]
NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
}
.responseData { response in
......
}
}
In your viewcontroller, replace the function with following code:
#objc func handleDownloadProgressNotification(notification:Notification) {
var dateDays = [Date]()
guard let info = notification.userInfo,
let date = info["date"] as? Date,
let index = info["index"] as? Int,
let progress = info["progress"] as? Double,
let section = dateDays.index(where: {$0 == date})
else {return}
let indexPath = IndexPath(item: index, section: section)
}
In the download function we are passing the date and index of the row from where you started the download and you are returning it back with notification. you can also send section and row index to download function. it's mainly upto you how you want to track the row. you could've also set delegate instead of notification to track the download progress
I want to improve the MPCRevisited project which is Chat app that using multi peer method. I'm using BLE to connect one device to another device (iPad and iPod) and send and receive the data. However, when I press home button to make background mode on one device, after 5 seconds, I can't send or receive the data.
image description here
I've already check all the thing in background modes, but still its not working at all.
import UIKit
import MultipeerConnectivity
class ParkBenchTimer {
let startTime:CFAbsoluteTime
var endTime:CFAbsoluteTime?
init() {
startTime = CFAbsoluteTimeGetCurrent()
}
func stop() -> CFAbsoluteTime {
endTime = CFAbsoluteTimeGetCurrent()
return duration!
}
var duration:CFAbsoluteTime? {
if let endTime = endTime {
return endTime - startTime
} else {
return nil
}
}
}
class ChatViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var chatTextField: UITextField!
#IBOutlet weak var chatTableView: UITableView!
var messagesArray: [[String : String]] = []
let mpcManager = MPCManager.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.chatTableView.delegate = self
self.chatTableView.dataSource = self
self.chatTableView.estimatedRowHeight = 60.0
self.chatTableView.rowHeight = UITableViewAutomaticDimension
self.chatTextField.delegate = self
self.mpcManager.messageRecievedDelegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// MARK: IBAction method implementation
#IBAction func endChat(sender: AnyObject) {
let messageDictionary: [String: String] = ["message": "_end_chat_"]
if self.mpcManager.sendData(dictionaryWithData: messageDictionary, toPeer: self.mpcManager.session.connectedPeers[0] as MCPeerID){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
self.mpcManager.session.disconnect()
})
}
}
// MARK: UITableView related method implementation
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messagesArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCellWithIdentifier("idCell") else {
assert(true)
return UITableViewCell()
}
guard let currentMessage = self.messagesArray[safe: indexPath.row] else {
print(" ")
assert(true)
return UITableViewCell()
}
if let sender = currentMessage["sender"] {
var senderLabelText: String
var senderColor: UIColor
if sender == "self" {
senderLabelText = "I said:"
senderColor = UIColor.purpleColor()
} else {
senderLabelText = sender + " said:"
senderColor = UIColor.orangeColor()
}
cell.detailTextLabel?.text = senderLabelText
cell.detailTextLabel?.textColor = senderColor
}
if let message = currentMessage["message"] {
cell.textLabel?.text = message
}
return cell
}
// MARK: UITextFieldDelegate method implementation
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
guard let textFieldText = textField.text else {
assert(true)
return false
}
let messageDictionary: [String: String] = ["message": textFieldText]
guard let connectedPeer = self.mpcManager.session.connectedPeers[safe: 0] else {
print(" ")
assert(true)
return false
}
if self.mpcManager.sendData(dictionaryWithData: messageDictionary, toPeer: connectedPeer) {
let dictionary = ["sender": "self", "message": textFieldText]
self.messagesArray.append(dictionary)
self.updateTableview()
} else {
print("Could not send data")
}
textField.text = ""
return true
}
// MARK: Custom method implementation
func updateTableview(){
chatTableView.reloadData()
if self.chatTableView.contentSize.height > self.chatTableView.frame.size.height {
let indexPathToScrollTo = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
self.chatTableView.scrollToRowAtIndexPath(indexPathToScrollTo, atScrollPosition: .Bottom, animated: true)
}
}
}
extension ChatViewController : MPCManagerRecievedMessageDelegate {
func managerRecievedData(data:NSData ,fromPeer:MCPeerID) {
// Convert the data (NSData) into a Dictionary object.
let dataDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! [String : String]
// Check if there's an entry with the "message" key.
if let message = dataDictionary["message"] {
// Make sure that the message is other than "_end_chat_".
if message != "_end_chat_"{
// Create a new dictionary and set the sender and the received message to it.
let messageDictionary: [String: String] = ["sender": fromPeer.displayName, "message": message]
// Add this dictionary to the messagesArray array.
messagesArray.append(messageDictionary)
// Reload the tableview data and scroll to the bottom using the main thread.
self.updateTableview()
} else {
}
}
}
func managerDidRecievedMessage(message: String, fromPeer: MCPeerID) {
// Create a new dictionary and set the sender and the received message to it.
//let messageDictionary: [String: String] = ["sender": fromPeer.displayName, "message": message]
// Add this dictionary to the messagesArray array.
//messagesArray.append(messageDictionary)
// Reload the tableview data and scroll to the bottom using the main thread.
//self.updateTableview()
}
func managerDidEndChat(fromPeer:MCPeerID) {
// In this case an "_end_chat_" message was received.
// Show an alert view to the user.
let alert = UIAlertController(title: "", message: "\(fromPeer.displayName) ended this chat.", preferredStyle: UIAlertControllerStyle.Alert)
let doneAction: UIAlertAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
self.mpcManager.session.disconnect()
self.dismissViewControllerAnimated(true, completion: nil)
}
alert.addAction(doneAction)
self.presentViewController(alert, animated: true, completion: nil)
}
}
This is my code.
Please help me if someone knows this problem. What I want to do is one device to keep sending the message and other device to become background and foreground back and forth.
Thank you.
Looking at some other StackOverflow posts (here and here), it seems like the Multipeer Connectivity Framework is not built to function in the background and your functionality will disappear after a couple minutes.
Bluetooth will function in the background, with the capabilities that you checked, but you will have to create your own messaging platform; even though Multipeer relies partially on Bluetooth, the capabilities are separate entities.
I am making a cocktail iOS application.
I am adding strings to a tableview (an ingredient to the "cabinet"). The user enters an ingredient and then presses the button ADD. It successfully adds it to the Core Data but it does not appear right away. What am I doing wrong?
Below is my code, thanks!
ViewController:
import UIKit
import CoreData
class CabinetViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var ingredientArray = [String]()
var display = [String]()
var dbIngredients = [String]()
let ingredientFetch = NSFetchRequest(entityName: "Cabinet")
var fetchedIngredient = [Cabinet]()
#IBOutlet weak var TextUI: UITextField!
#IBOutlet weak var Button: UIButton!
#IBOutlet weak var TableView: UITableView!
let moc = DataController().managedObjectContext
override func viewDidLoad() {
super.viewDidLoad()
TextUI.delegate = self
TextUI.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
TableView.delegate = self
TableView.dataSource = self
TableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
// fetch Core Data
do{
fetchedIngredient = try moc.executeFetchRequest(ingredientFetch) as! [Cabinet]
} catch {
fatalError()
}
let postEndpoint: String = "http://www.thecocktaildb.com/api/json/v1/1/list.php?i=list"
guard let url = NSURL(string: postEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = NSURLRequest(URL: url)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
}
guard error == nil else {
print("error calling GET on www.thecocktaildb.com")
print(error)
return
}
let post: NSDictionary
do {
post = try NSJSONSerialization.JSONObjectWithData(responseData,
options: []) as! NSDictionary
} catch {
print("error trying to convert data to JSON")
return
}
var count = 1
if let drinks = post["drinks"] as? [NSDictionary] {
for drink in drinks {
if let strIngredient = drink["strIngredient1"] as? String {
print(String(count) + ". " + strIngredient)
self.dbIngredients.append(strIngredient)
count++
}
}
}
})
task.resume()
TableView.reloadData()
}
func textFieldDidChange(textField: UITextField) {
search(self.TextUI.text!)
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
Button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
return true
}
func buttonPressed(sender: UIButton!) {
//ingredientArray.append(TextUI.text!)
let entity = NSEntityDescription.insertNewObjectForEntityForName("Cabinet", inManagedObjectContext: moc) as! Cabinet
entity.setValue(TextUI.text!, forKey: "ingredient")
do{
try moc.save()
}catch {
fatalError("failure to save context: \(error)")
}
showAlertButtonTapped(Button)
// dispatch_async(dispatch_get_main_queue(), { () -> Void in
// self.TableView.reloadData()
// })
}
#IBAction func showAlertButtonTapped(sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "Added!", message: "You've added " + TextUI.text! + " to your cabinet", preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(alert, animated: true, completion: nil)
}
func search(str:String) {
display.removeAll(keepCapacity: false)
for s in dbIngredients{
if s.hasPrefix(str){
display.append(s)
print(s)
}
}
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return fetchedIngredient.capacity
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
do{
let fetchedIngredient = try moc.executeFetchRequest(ingredientFetch) as! [Cabinet]
cell.textLabel?.text = fetchedIngredient[indexPath.row].ingredient
} catch {
fatalError("bad things happened: \(error)")
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let alert = UIAlertController(title: "Remove " + fetchedIngredient[indexPath.row].ingredient!,
message: "No more " + fetchedIngredient[indexPath.row].ingredient! + " in your cabinet?",
preferredStyle: .Alert)
let deleteAction = UIAlertAction(title: "Remove",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
self.fetchedIngredient.removeAtIndex(indexPath.row)
do{
let fetchedResults = try self.moc.executeFetchRequest(self.ingredientFetch)
if let result = fetchedResults[indexPath.row] as? NSManagedObject {
self.moc.deleteObject(result)
try self.moc.save()
}
}catch{
fatalError()
}
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addAction(cancelAction)
alert.addAction(deleteAction)
presentViewController(alert,
animated: true,
completion: nil)
TableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Since your problem isn't Core Data you need to use Table View beginUpdates and EndUpdates to insert the row. At the end of your buttonPressed function put this:
do{
fetchedIngredient = try moc.executeFetchRequest(ingredientFetch) as! [Cabinet]
self.tableView.beginUpdates()
let totalIngredients = fetchedIngredient.count
let newItemIndexPath = NSIndexPath(forRow: totalIngredients-1, inSection: 0)
self.tableView.insertRowsAtIndexPaths([newItemIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()
} catch {
fatalError()
}
On your number of rows in section:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedIngredient.count
}
And on the cell for row at index path
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = fetchedIngredient[indexPath.row].ingredient
return cell
}
There are a couple of problems with your code. Firstly, since you're fetching records into an array, calling reloadData will not have any impact unless you update the array. There is no automatic connection between adding a new core data record and your fetchedIngredient array.
There are a few ways to solve this, although the most common is probably to just refetch the records into the same array whenever core data is updated. Alternatively you can change your code to us NSFetchedResultsController instead of an array, which will automatically update the tableView when core data is updated (based on the predicate you provide it). This class provides the automatic connection to core data for you.
Another problem is that you are refetching the records in cellForRowAtIndexPath and didSelectRowAtIndexPath. This should not be done. Instead you should just be referring to the class-level fetchedIngredient array (or the NSFetchedResultsController if you choose to use that).
Furthermore, the call to dataTaskWithRequest runs in the background. It's not clear from the code how you're using it, but the fact that you have reloadData afterwards suggests it should impact the tableView. However because the task runs in the background, the completion handler will run after the table is reloaded. Therefore you should be calling reloadData inside the completion handler. And since it would then be running on another thread, you would have to dispatch it to the main queue, using:
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
This question already has an answer here:
Core Data Update in swift while selecting any row in list table view not working?
(1 answer)
Closed 7 years ago.
I have 2 VC in my core data project. In VC1 I have a table view for displaying the names in my coredata database. And in my VC2 I enter the details for the entity. Now I want to update my database when user selects a particular row. When user selects a row it would move to the VC2 and display the name in the textfield and if the user updates anything in it,it would update. I have my code below. Please suggest what I should do next.
Entering the details-
import UIKit
import CoreData
class EnterDetailViewController: UIViewController {
#IBOutlet weak var nametextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func savedata(sender: AnyObject)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedcontext = appdelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedcontext)
let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedcontext)
person.setValue(self.nametextfield.text, forKey: "name")
do
{
try managedcontext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
VC1- Displaying in table view
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableview: UITableView!
var people = [NSManagedObject]()
let manai = UIApplication.sharedApplication().delegate as! AppDelegate
//var person :NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool)
{
print("CALLING FETCHDATA")
self.fetchData()
print("RELOADING DATA")
self.tableview.reloadData()
}
func fetchData()
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Person")
do
{
people = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
print(people)
print("FETCHING DATA")
}
catch let error as NSError
{
print("could not fetch \(error), \(error.userInfo)")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return people.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
let person = people[indexPath.row]
cell.textLabel?.text = person.valueForKey("name") as? String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
if editingStyle == .Delete
{
managedContext.deleteObject(people[indexPath.row])
}
do
{
try managedContext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
}
fetchData()
self.tableview .reloadData()
print("reload after delete")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "segueupdate"
{
}
}
}
The designated initializer of NSManagedObject is
initWithEntity:insertIntoManagedObjectContext:. You can't call the generic initializer NSManagedObject().
In your case you could declare managedobjectt as (implicit unwrapped) optional
var managedobjectt = NSManagedObject!
To make sure that accessing the variable doesn't happen too soon in VC2 you could also move the code in viewDidLoad() to viewWillAppear()
Here is my problem, I'm trying to make a clone of Snapchat, a very simple one, using Parse.com with the default project given in the page, everything was going ok, until I have to take pictures and save them to send them to an user. With this code:
import UIKit
import Parse
class UsersTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var usersArray: [String] = []
var activeRecipient: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
var query = PFUser.query()
query?.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
var users = query?.findObjects()
if let user = users {
for username in user {
println(username.username!!)
usersArray.append(username.username!!)
}
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return usersArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("snapCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = usersArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
activeRecipient = indexPath.row
pickImage()
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
//Upload image to parse //error come somewhere from here I think
println("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
var imageToSend = PFObject(className:"Image")
imageToSend["image"] = UIImageJPEGRepresentation(image, 0.5)
imageToSend["senderUsername"] = PFUser.currentUser()!.username
imageToSend["recipientUsername"] = usersArray[activeRecipient]
imageToSend.save()
}
func pickImage() {
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
}
The error:
2015-06-24 15:03:13.414 SnapClomSwift[2043:124661] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData PF_base64EncodedString]: unrecognized selector sent to instance 0x7a648960'
Is not very helpful to debug, any help??
EDIT1: I would think the parse function is the last calling and breaking everything but I'm not sure.
EDIT2: I have I guess fixed but I'm not sure what was in the first place, still. The new code is this:
var imageToSend = PFObject(className:"Image")
//var imageData = UIImagePNGRepresentation(image)
imageToSend["senderUsername"] = PFUser.currentUser()!.username!
imageToSend["recipientUsername"] = usersArray[activeUser]
imageToSend.saveInBackgroundWithBlock({
(success: Bool, error: NSError?) -> Void in
if (success == false) {
// Error.
println("Error horray! \(error?.description)")
} else {
// There was a problem, check error.description
let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name: "image-png", data: imageData)
imageToSend["image"] = imageFile
imageToSend.saveInBackgroundWithBlock({
(success: Bool, error: NSError?) -> Void in
if success == false {
println("something is fucked up \(error?.description)")
} else {
println("Cool")
}
})
println("Done")
}
})
I guess this is not a solution but a workaround so I'm going to accept Zaph's answer.
To see the actual statement that is causing the error add an exception breakpoint:
From the Mian Menu Debug:Breakpoints:Create Exception Breakpoint.
Right-click the breakpoint and set the exception to Objective-C.
Add an action: "po $arg1".
Run the app to get the breakpoint and you will be at the line that causes the exception and the error message will be in the debugger console.