How to binding data in swift? - swift

I'm going to show the view the data I got from the server. An error occurred while developing using Rxswift and MVVM. The id value must be received from the product model, and an error occurs in this part.
extension SelectViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("Tap")
let detailVC = DetailViewController()
detailVC.product = products[indexPath.row]
detailVC.productId = products[indexPath.row].id
self.navigationController?.pushViewController(detailPassVC, animated: true)
}
}
This is the code for the part that passes the id value. detailVC.product = products[indexPath.row]An error occurs in this area. error message is "Thread 1: Fatal error: Index out of range"
var products:[ProductModel] = []
var product = ProductModel()
var productId = Int()
func initViewModel() {
let input = DetailViewModel.Input(loadDetailData: getData.asSignal(onErrorJustReturn: ()), id: productId)
let output = viewModel.transform(input: input)
output.loadDetail.asObservable().bind(onNext: { data in
self.infoLabel.text = data?.detailDescription
self.passView.setData(data!)
self.secondView.setData(data!)
self.fareView.setData(data!)
self.totalLabel.text = totalPrice(data!)
}).disposed(by: disposeBag)
}
This is the code for the part where the view model is bound.
And This is my viewmodel code
class DetailViewModel: BaseViewModel {
private let repo = ProductRepository()
private let disposeBag = DisposeBag()
struct Input {
let loadDetailData: Signal<Void>
let id: Int
}
struct Output {
let loadDetail: Driver<DetailModel?>
let error: Driver<String?>
}
func transform(input: Input) -> Output {
let loadDetail = PublishSubject<DetailModel?>()
let msg= PublishSubject<String?>()
input.loadDetailData.asObservable()
.flatMap { self.productRepo.detailProduct(input.id)}
.subscribe(onNext: { data in
switch data {
case let .success(detail):
loadDetail.onNext(detail)
default:
errorMessage.onNext("load fail")
}
}).disposed(by: disposeBag)
return Output(loadDetail: loadDetail.asDriver(onErrorJustReturn: nil),error: msg.asDriver(onErrorJustReturn: nil))
}
}
How can I send the ID value?

Related

errors in swift Value of type 'URLSession' has no member 'request'

I am receiving three errors in my viewController and I can not figure out how to fix them these errors are 1.) extension Declaration is only valid at file scope, 2.)Value of type 'Unable to infer type of a closure parameter 'result' in the current context ' 3.) URLSession has no member 'request'. None of these errors make any sense to me because I have already defined result as a member of URLSession. Im not sure if Xcode is glitching, or if these errors are present in my code and if so can anyone point them out and how to fix?
import UIKit
import Foundation
enum NetworkError: Error {
case badRequest
case decodingError
}
struct Category: Codable {
let CategoryId: String
let CategoryThumb: String
let CategoryDescription: String
let categories: [Category]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
struct Constants {
static let categoriesurl = URL(string: "https://www.themealdb.com/api/json/v1/1/categories.php")
static let filterListurl = URL(string: "https://www.themealdb.com/api/json/v1/1/filter.php?c=Beef")
static let mealByIdurl = URL(string: "https://www.themealdb.com/api/json/v1/1/lookup.php?i=52874")
}
//create table view to test results from API endpoint
let table: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
private var categories: [Category] = []
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(table)
table.delegate = self
table.dataSource = self
fetch()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
table.frame = view.bounds
}
func fetch() {
URLSession.shared.request(url: Constants.categoriesurl, returning: [Category].self
) {[weak self] result in
switch result {
case.success(let category):
DispatchQueue.main.async {
self?.category = category
self?.table.reloadData()
}
case.failure(let error):
print(error )
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = categories[indexPath.row].CategoryId
return cell
}
}
extension URLSession {
func request<T: Codable>(url: URL?,returning: T.Type,completion: #escaping (Result<T, Error>) -> Void) {
guard let url = url else {
//we create a enum for network error that will return an error if the API end point is a bad request,or if unable decoding the Json data
//Create a urls that fetches the data from each API end points
NetworkError.badRequest
return
}
let task = dataTask(with: url) {data, _, error in
guard let data = data else {
if let error = error {
NetworkError.badRequest
} else {
NetworkError.decodingError
}
return
} //if your data is return sucessfully from the API
do {
let Result = try! JSONDecoder().decode(returning, from: data)
completion(.success(Result))
}
catch {
NetworkError.badRequest
} //if the data does not return an error will be met
}
task.resume()
}
}
}
You're declaring an extension within the scope of a struct declaration. Your extension has to be declared outside of any other scope.
Because you extension was rejected, The system doesn't know what kind of parameter request should take and since you haven't declared the type of result inside the closure, it has no way to infer what what type it should have and the error message tells you just that
URLSession does not have a member named request because your extension was rejected (because of error #1).
The first step to fix this would be to move your extension outside of the scope of struct Category
extension Declaration is only valid at file scope -
Extension go inside a struct or a class, here you are declaring urlSession extension inside Category struct, please declare it outside.
Value of type 'Unable to infer type of a closure parameter 'result' in the current context ' - Please let us know the line of error, mostly this type of error would require to handle do-try-catch.
When the extension is placed outside the struct, this error would go.

Tableview Updating wrong cell

I made a like button problem is when the like button is tapped the values update only the first tableview cell even if I clicked the second cell it will update the values of likes in the first tableview cell. The values update properly on firestore for each individual cell but its being loaded wrong
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
motivationThoughts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DailyThoughtCELL", for: indexPath) as? dailyMotivationTableViewCell
cell!.generateCellsforDailymotivation(_MotivationdataMODEL: self.motivationThoughts[indexPath.row], objectID: self.motivationThoughts[indexPath.row].motiveID)
//cell!.postID = self.motivationThoughts[indexPath.row].motiveID //this works and updates the value
//cell!.generateLikeCell(objectID: self.motivationThoughts[indexPath.row].motiveID)
return cell!
}
func loaddailymotivation() {
FirebaseReferece(.MotivationDAILY).addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) { // this line means if the chage that happened in the document was equal to added something
let data = diff.document.data()
print("we have\(snapshot.documents.count) documents in this array")
let dailyMotivationID = data["objectID"] as! String
let dailymotivationTitle = data["Motivation title"] as! String //calls the data thats heald inside of motivation title in firebase
let dailyMotivationScripture = data["daily motivation scripture"] as! String //calls the data thats heald inside of Motivation script in firebase
let dailyMotivationNumberOfLikes = data["Number of likes in daily motivation post"]as! Int
let MdataModel = motivationDailyModel(RealMotivationID: dailyMotivationID, RealmotivationTitle: dailymotivationTitle, RealmotivationScrip: dailyMotivationScripture, RealmotivationNumberOfLikes: dailyMotivationNumberOfLikes)
self.motivationThoughts.append(MdataModel)
}
/*
I thinkyou need a method that identifies the item that needs to be modified in the array, and replace/modify it. If you add a new object, you have one more element in your table*/
if (diff.type == .modified) {
print("Modified data: \(diff.document.data())")
let newdata = diff.document.data()
let objectID = newdata["objectID"] as! String // we get the object id of the uodated item
//self.GrabThatDamnstring(grabIt: objectID)
//self.incrementLikes(NewobjectID: objectID) //write a function to grab the string from the certain post and then send it to dailymotivationviewcell into the function increment likes so so it can update the specific objectID
guard let dailymotivationIndex = self.motivationThoughts.firstIndex(where: {_ in objectID == objectID}) else { return }
var dailymotivation = self.motivationThoughts[dailymotivationIndex]
let dailyMotivationNumberOfLikes = newdata["Number of likes in daily motivation post"] as! Int
dailymotivation.motivationNumberOfLikes = dailyMotivationNumberOfLikes
self.motivationThoughts[dailymotivationIndex] = dailymotivation
// here you will receive if any change happens in your data add it to your array as you want
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
}
}
this is the uitableview cellcode
import UIKit
import FBSDKLoginKit
import Firebase
import JGProgressHUD
class dailyMotivationTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//numberOfLikesGenerator()
self.holdView.layer.cornerRadius = 19
self.likedbuttonFIlled.isHidden = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBOutlet weak var holdView: UIView!
#IBOutlet weak var likedbuttonFIlled: UIImageView!
#IBOutlet weak var likeSavedButton: UIButton!
#IBOutlet weak var DMtextLabel: UILabel!
#IBOutlet weak var DMtitleLabel: UILabel!
#IBOutlet weak var numberOfLikesLabel: UILabel!
//MARK: VARS/LETS
var postID : String!
var MotivationData : motivationDailyModel!
let hud = JGProgressHUD(style: .light)
//MARK: IBACTIONS
#IBAction func likeButtonTapped(_ sender: Any) {
if (Auth.auth().currentUser != nil || AccessToken.current != nil) {
changeLikeButtonMode()
//update like number and update like number on firebase
}
else{
hud.textLabel.text = "Please Login to Continue!"
hud.show(in: self.contentView)
hud.dismiss(afterDelay: 3.0)
hud.indicatorView = JGProgressHUDErrorIndicatorView()
//no user login so pull up login view
}
//MARK: TODO when this button is tapped ALSO WANT TO STORE THIS SNAPSHOT INTO AN ARRAY THAT WE WILL SHOW IN OUR SAVED VIEW CONTROLLEr
}
//call this function from motivation daily viewcontroller and it will also have the item that was tapped
func incrementLikes(){
//MARK: WE NEED TO FIND OUT HOW WE GET THE UUID FOR THE CERTAIN POS
FirebaseReferece(.MotivationDAILY).document(postID).updateData(["Number of likes in daily motivation post":FieldValue.increment(Int64(1))]) { (error) in
if error != nil {
print(error!.localizedDescription)
} else {
print("successfully incremented data!")
}
}
}
func changeLikeButtonMode(){
// so if likedbutton is tapped and the heart isnt red that means that the tag is = 0 so its gnna show the red heard and then also change the tag to 1 but when it is tapped again its going to change the tag to 0 and removed the red heart
if likeSavedButton.tag == 0 //means its empty
{
incrementLikes()
self.likedbuttonFIlled.isHidden = false
likeSavedButton.tag = 1
}else {
self.likedbuttonFIlled.isHidden = true
likeSavedButton.tag = 0
}
}
//MARK: FUNCTIONS
func generateCellsforDailymotivation(_MotivationdataMODEL : motivationDailyModel,objectID : String!) {
DMtextLabel.text = _MotivationdataMODEL.motivationDailyScripture
DMtitleLabel.text = _MotivationdataMODEL.motivationTitle
numberOfLikesLabel.text = "\(String(_MotivationdataMODEL.motivationNumberOfLikes))"
postID = objectID
}
}
The condition in your guard statement always evaluates to true
guard let dailymotivationIndex = self.motivationThoughts.firstIndex(where: {_ in objectID == objectID}) else { return }
Try this instead
guard let dailymotivationIndex = self.motivationThoughts.firstIndex(where: { $0.RealMotivationID == objectID}) else { return }

Differences in work of saveContext() depending of viewController instantination.CoreData Enigma

The project has a minimalistic CoreData Stack.
I have one UIViewController subclass "AddOrChangeEntityVC" to create NSManagedObject, change its properties and write data in DB.
The listed functions work fine if "AddOrChangeEntityViewController" was created from
MyFirstVC, MySecondVC, ... and MyNinthVC any other viewcontroller exept one BadVC.
In this case an attempt is made to write an Entity into DB with all properties = nil.
And i see this error.
Error Domain=NSCocoaErrorDomain Code=1560 "Multiple validation errors occurred."
And for every required property something like
"Error Domain=NSCocoaErrorDomain Code=1570 \"firstProperty is a required value.\
UserInfo={NSValidationErrorObject=<APP.Entity: 0x600006917e80> (entity: Entity; id: 0x600000f152a0 <x-coredata:///Entity/t72AEC6D7-A854-40A3-BA9D-5830DFADC8AF2>; data: {\n firstProperty = nil;\n secondProperty = nil;\n}), NSValidationErrorKey = firstValue, NSLocalizedDescription=firstValue is a required value"
"Error Domain=NSCocoaErrorDomain Code=1570 \"secondProperty is a required value.\"
It should be noted that before saveContext() call all properties are validated.
AddOrChangeEntityVC: UIViewController {
var entity: Entity?
func saveEntity() -> Bool {
//validating data
if !validateData() {
return false
}
//creating passenger
if entity == nil {
entity = Entity()
}
//saving entity
if let entity = entity {
//saving
entity.firstProperty = firstPropertyTextfield.text!
entity.secondProperty = secondPropertyTextfield.text!
...
CoreDataStack.shared.saveContext()
return true
}
}
Entity: NSManaged
class Entity: NSManagedObject {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Entity> {
return NSFetchRequest<Entity>(entityName: "Entity")
}
#NSManaged public var firstProperty: String
#NSManaged public var secondProperty: String
convenience init() {
let entity = NSEntityDescription.entity(forEntityName: "Entity",
in: CoreDataStack.shared.persistentContainer.viewContext)!
self.init(entity: entity, insertInto: CoreDataStack.shared.persistentContainer.viewContext)
}
}
CoreData Manager
class CoreDataStack {
static var shared = CoreDataStack()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "APP")
container.loadPersistentStores { storeDesription, error in
guard error == nil else {
fatalError("Unresolved error \(error!)")
}
}
// Merge the changes from other contexts automatically. useŠ² for Another Entity
enter code here
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.shouldDeleteInaccessibleFaults = true
return container
}()
func saveContext() {
let context = persistentContainer.viewContext
guard context.hasChanges else { return }
do {
try context.save()
} catch let error as NSError{
print("Error: \(error), \(error.userInfo)")
}
}
}
Transition from anyVC to AddOrChangeEntityVC
guard let entityVC = self.storyboard?.instantiateViewController(withIdentifier: "addEntityVC") as? AddOrChangeEntityVC else {return}
self.navigationController?.pushViewController(entityVC, animated: true)
```
Well, Core Data is misterious and even obscurant.
I made this simple steps.
1) in AddOrChangeEntityVC i added
var context: NSManagedObjectContext = CoreDataStack.shared.persistentContainer.newBackGroundContext()
2) in initialization of Entity: NSManaged (if we need to create it)
if entity == nil {
entity = Entity(context: context)
}
3) unexpectedly facing that after some saving in NSFetchingResultController results added new misterious entity with all properties == nil i filtered results in tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int method and tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell by removing from fetchedResultsController.fetchedObjects? elements with some properties == nil.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.fetchedObjects?.filter({!$0.firstProperty.isEmpty}).count ?? 0
}
and
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if fetchedResultsController.object(at: indexPath).isValid {
let entity = fetchedResultsController.object(at: indexPath)
let cell = UITableViewCell()update(with: entity)
....
}

UITableView with Segmented Controller not displaying data immediately - Swift

I have a Tab Bar Controller, and one of the two views from it is a TableViewController. The TableViewController has a SegmentedControl bar on the top of the table to switch between two datasets pulled from Firebase.
When I run the app, the table doesn't show any data straight away, only when I switch to the other segment of the SegmentControl bar. But, when I switch back, the data for the first segments loads.
I put in a breakpoint to see what was happening, and it was skipping over the code I wrote to pull the data from Firebase, so the arrays were empty upon initial loading, hence the lack of data. Yet, when I switch segments to the other option, the data appears.
I'm also trying to sort the data within the arrays, and this doesn't do anything at all because it runs when the arrays come back empty, so there is nothing to sort.
My code is:
class LeaderboardViewController: UITableViewController
{
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet var leaderboardTable: UITableView!
var countyRef = Database.database().reference().child("countyleaderboard")
var schoolRef = Database.database().reference().child("schoolleaderboard")
var refHandle: UInt!
var countyList = [County]()
var schoolList = [School]()
override func viewDidLoad()
{
super.viewDidLoad()
fetchData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var sectionCount = 0
switch(segmentedControl.selectedSegmentIndex)
{
case 0:
sectionCount = countyList.count
break
case 1:
sectionCount = schoolList.count
break
default:
break
}
return sectionCount
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let boardCell = tableView.dequeueReusableCell(withIdentifier: "boardCell", for: indexPath)
switch(segmentedControl.selectedSegmentIndex)
{
case 0:
boardCell.textLabel!.text = "" + countyList[indexPath.row].name! + ": \(countyList[indexPath.row].score!)"
break
case 1:
boardCell.textLabel!.text = "" + schoolList[indexPath.row].name! + ": \(schoolList[indexPath.row].score!)"
break
default:
break
}
return boardCell
}
func fetchData()
{
schoolRef.observe(.childAdded, with:
{ snapshot in
if let dictionary = snapshot.value as? [String: AnyObject]
{
let school = School()
school.name = dictionary["name"] as! String
school.score = dictionary["score"] as! Float
self.schoolList.append(school)
}
}, withCancel: nil)
self.schoolList.sort(by: { $0.score > $1.score })
countyRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children
{
let snap = child as! DataSnapshot
let county = County()
county.name = snap.key as String
county.score = snap.value as! Float
self.countyList.append(county)
}
})
self.countyList.sort(by: { $0.score > $1.score })
DispatchQueue.main.async
{
self.leaderboardTable.reloadData()
}
}
#IBAction func segmentedControlChanged(_ sender: Any)
{
DispatchQueue.main.async
{
self.leaderboardTable.reloadData()
}
}
}
My questions are:
Why does the data not load straight away?
Where are the arrays coming from if they are not being populated with data on the first run? And why are they not being sorted if that code is directly below the code that populates them?
The dispatch block should be after the for loop and before the }).
It should be within the observe/observeSingleEvent. Also your sort code needs to be inside the same block. Like below:
countyRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children
{
let snap = child as! DataSnapshot
let county = County()
county.name = snap.key as String
county.score = snap.value as! Float
self.countyList.append(county)
}
self.countyList.sort(by: { $0.score > $1.score })
DispatchQueue.main.async
{
self.leaderboardTable.reloadData()
}
})

FetchedResultsController Swift 3 API Misuse: Attempt to serialize store access on non-owning coordinator

I'm attempting to use a fetchedResultsController to handle the results in my UITable.
It works initially when the program starts up. Then when I switch back to the inventory tab where my table is (for the viewToAppear again), this is when it crashes.
I'm getting a runtime crash error in my viewWillAppear() method of the window which has the table.
In particular it crashes on the Inventory+CoredataProperties.swift file on this line let characters = name!.characters.map { String($0) }, but I suspect the error is somewhere else as this works initially so why not now on the 2nd reload?
Here is the function.
override func viewWillAppear(_ animated: Bool) {
print("view appearing")
//When the view appears its important that the table is updated.
//Trigger Event on SearchBar in case returning from BarCode Scanner
// self.searchBar:SearchBar textDidChange:recentlySearchedWord;
//searchBar.performSelector(":textDidChange")
//Perform another fetch again to get correct data~
do {
//fetchedResultsController. //this will force setter code to run again.
print("attempting fetch again, reset to use lazy init")
fetchedResultsController = setFetchedResultsController() //sets it again so its correct.
try fetchedResultsController.performFetch()
} catch {
print("An error occurred")
}
inventoryTable.reloadData()//this is important to update correctly for changes that might have been made
}
The error occurs on the try fetchedResultsController.performFetch() statement. I'm getting a lot of errors before the actual crash occurs saying "API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x170265300, store PSC = 0x0). I've been refactoring my code to work with the new swift 3 standards I have a feeling I did something wrong or maybe something changed with how the fetched results controller works.
Any help is appreciated as to what could be the cause?
If you think I'm missing a file you need to see, just let me know and I'll add it to the relevant source code below.
POSSIBLE RELEVANT SOURCE CODE BELOW:
InventoryController.swift (Entire File)
import UIKit
import CoreData
import Foundation
class InventoryController: UIViewController, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
#available(iOS 2.0, *)
//Create fetchedResultsController to handle Inventory Core Data Operations
lazy var fetchedResultsController: NSFetchedResultsController<Inventory> = {
return self.setFetchedResultsController()
}()
//Reference to search text for filtering
var m_searchText = ""
func setFetchedResultsController() -> NSFetchedResultsController<Inventory>{
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let inventoryFetchRequest : NSFetchRequest<Inventory> = Inventory.fetchRequest()
var primarySortDescriptor = NSSortDescriptor(key: "name", ascending: true)//by default assume name.
print("primarySortDescriptor...")
if(g_appSettings[0].indextype=="numberfront"){
primarySortDescriptor = NSSortDescriptor(key: "barcode", ascending: true)
}else if(g_appSettings[0].indextype=="numberback"){
primarySortDescriptor = NSSortDescriptor(key: "barcodeReverse", ascending: true)
}else if(g_appSettings[0].indextype=="numberfourth"){
primarySortDescriptor = NSSortDescriptor(key: "barcodeFourth", ascending: true, selector: #selector(NSString.localizedCompare(_:)))
}
print("set primarySortDescriptor")
//let secondarySortDescriptor = NSSortDescriptor(key: "barcode", ascending: true)
inventoryFetchRequest.sortDescriptors = [primarySortDescriptor]
print("set sort descriptors to fetch request")
var storefilter : Store? = nil
var predicate: NSPredicate
//Store should never be set to nil, the first store should always be selected by default. For fringe cases just in case ... support added so doesn't break
if(g_appSettings[0].selectedStore != nil){
storefilter = g_appSettings[0].selectedStore
predicate = NSPredicate(format: "store = %#", storefilter!) //default predicate assuming store is selected
//However if search text is present then modify predicate
if(m_searchText != ""){
predicate = NSPredicate(format: "store = %# AND name contains[cd] %# OR store = %# AND barcode contains[cd] %#", storefilter!,m_searchText,storefilter!,m_searchText)
}
//This will ensure correct data relating to store is showing (and if any filters present, them as well)
inventoryFetchRequest.predicate = predicate
}else{
if(m_searchText != ""){
predicate = NSPredicate(format: "name contains[cd] %# OR barcode contains[cd] %#",m_searchText,m_searchText)
inventoryFetchRequest.predicate = predicate
//This will ensure correct data relating to store is showing
}
}
//default assume letter section
var frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: "lettersection",
cacheName: nil)
if(g_appSettings[0].indextype=="numberfront"){
frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: "numbersection",
cacheName: nil)
}else if(g_appSettings[0].indextype=="numberback"){
frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: "numberendsection",
cacheName: nil)
}else if(g_appSettings[0].indextype=="numberfourth"){
frc = NSFetchedResultsController(
fetchRequest: inventoryFetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: "numberfourthsection",
cacheName: nil)
}
print("set the frc")
frc.delegate = self
return frc
}
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var inventoryTable: UITableView!
// Start DEMO Related Code
var numberIndex = ["0","1","2","3","4","5","6","7","8","9"]
var letterIndex = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var previousNumber = -1 //used so we show A,A, B,B, C,C etc for proper testing of sections
func createInventoryDummyData(number: Int) -> Inventory{
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let tempInventory = NSEntityDescription.insertNewObject(forEntityName: "Inventory", into: moc) as! Inventory
if(number-1 == previousNumber){
tempInventory.name = "\(letterIndex[number-2])-Test Item # \(number)"
previousNumber = -1//reset it again
}else{
tempInventory.name = "\(letterIndex[number-1])-Test Item # \(number)"
previousNumber = number //set previous letter accordingly
}
tempInventory.barcode = "00000\(number+1)00\(number)"
//special exception to demo barcode reader
if(number==5){
tempInventory.barcode = "0051111407592"
}
if(number==6){
tempInventory.barcode = "0036000291452"
}
tempInventory.barcodeReverse = String(tempInventory.barcode!.characters.reversed())
//Convert barcode into array of characters and take note if its size for indexing
let bcArraySize = Int(tempInventory.barcode!.characters.count) - 1//for correct indexing
var bcArray = tempInventory.barcode!.characters.map { String($0) }
print(bcArray)
print(bcArraySize)
//Take the digits from the 4th one at a time and convert to strings concatenating as you go.
let fourth = "\(bcArray[bcArraySize-3])"+"\(bcArray[bcArraySize-2])"+"\(bcArray[bcArraySize-1])"+"\(bcArray[bcArraySize])"
print(fourth)
//Finally convert that into a number again and set to barcodeFourth
tempInventory.barcodeFourth = fourth
print(tempInventory.barcodeFourth!)
//tempInventory.barcodeFourth =
//print(tempInventory.barcodeReverse)
tempInventory.currentCount = 0
tempInventory.id = number as NSNumber?
tempInventory.imageLargePath = "http://distribution.tech//uploads/inventory/7d3fe5bfad38a3545e80c73c1453e380.png"
tempInventory.imageSmallPath = "http://distribution.tech//uploads/inventory/7d3fe5bfad38a3545e80c73c1453e380.png"
tempInventory.addCount = 0
tempInventory.negativeCount = 0
tempInventory.newCount = 0
tempInventory.store_id = 1 //belongs to same store for now
//Select a random store to belong to 0 through 2 since array starts at 0
let lo = 0;
let hi = 2;
let aRandomInt = Int.random(range:lo...hi)
tempInventory.setValue(g_storeList[aRandomInt], forKey: "store") //assigns inventory to one of the stores we created.
return tempInventory
}
func createStoreDummyData(number:Int) -> Store{
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let tempStore = NSEntityDescription.insertNewObject(forEntityName: "Store", into: moc) as! Store
tempStore.address = "100\(number) lane, Miami, FL"
tempStore.email = "store\(number)#centraltire.com"
tempStore.id = number as NSNumber?
tempStore.lat = 1.00000007
tempStore.lng = 1.00000008
tempStore.name = "Store #\(number)"
tempStore.phone = "123000000\(number)"
return tempStore
}
// End DEMO Related Code
override func viewDidLoad() {
super.viewDidLoad()
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
print("InventoryController -> ViewDidLoad -> ... starting inits")
// // Do any additional setup after loading the view, typically from a nib.
// print("InventoryController -> ViewDidLoad -> ... starting inits")
//
//First check to see if we have entities already. There MUST be entities, even if its DEMO data.
let inventoryFetchRequest = NSFetchRequest<Inventory>(entityName: "Inventory")
//let storeFetchRequest = NSFetchRequest(entityName: "Store")
do {
let inventoryRecords = try moc.fetch(inventoryFetchRequest)
//Maybe sort descriptor here? But how to organize into sectioned array?
if(inventoryRecords.count<=0){
g_demoMode = true
print("No entities found for inventory. Demo mode = True. Creating default entities & store...")
//Reset the Stores
g_storeList = [Store]()
var store : Store //define variable as Store type
for index in 1...3 {
store = createStoreDummyData(number: index)
g_storeList.append(store)
}
//save changes for inventory we added
do {
try moc.save()
print("saved to entity")
}catch{
fatalError("Failure to save context: \(error)")
}
var entity : Inventory //define variable as Inventory type
for index in 1...52 {
let indexFloat = Float(index/2)+1
let realIndex = Int(round(indexFloat))
entity = createInventoryDummyData(number: realIndex)
g_inventoryItems.append(entity)
}
//Save the changes
(UIApplication.shared.delegate as! AppDelegate).saveContext()
print("finished creating entities")
}
}catch{
fatalError("bad things happened \(error)")
}
// //perform fetch we need to do.
// do {
// try fetchedResultsController.performFetch()
// } catch {
// print("An error occurred")
// }
print("InventoryController -> viewDidload -> ... finished inits!")
}
override func viewWillAppear(_ animated: Bool) {
print("view appearing")
//When the view appears its important that the table is updated.
//Trigger Event on SearchBar in case returning from BarCode Scanner
// self.searchBar:SearchBar textDidChange:recentlySearchedWord;
//searchBar.performSelector(":textDidChange")
//Perform another fetch again to get correct data~
do {
//fetchedResultsController. //this will force setter code to run again.
print("attempting fetch again, reset to use lazy init")
fetchedResultsController = setFetchedResultsController() //sets it again so its correct.
try fetchedResultsController.performFetch()
} catch {
print("An error occurred")
}
inventoryTable.reloadData()//this is important to update correctly for changes that might have been made
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
print("inventoryItemControllerPrepareForSegueCalled")
if segue.identifier == "inventoryInfoSegue" {
let vc = segue.destination as! InventoryItemController
vc.hidesBottomBarWhenPushed = true //hide the tab bar. This prevents crashing error from being on this page then syncing & returning.
if let cell = sender as? InventoryTableViewCell{
vc.inventoryItem = cell.inventoryItem //sets the inventory item accordingly, passing its reference along.
}else{
print("sender was something else")
}
}
}
// func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
// //This scrolls to correct section based on title of what was pressed.
// return letterIndex.indexOf(title)!
// }
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
//This is smart and takes the first letter of known sections to create the Index Titles
return self.fetchedResultsController.sectionIndexTitles
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let currentSection = sections[section]
return currentSection.numberOfObjects
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InventoryTableCell", for: indexPath as IndexPath) as! InventoryTableViewCell
print("IndexPath=")
print(indexPath)
let inventory : Inventory = fetchedResultsController.object(at: indexPath as IndexPath)
cell.inventoryItem = inventory
cell.drawCell() //uses passed inventoryItem to draw it's self accordingly.
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchedResultsController.sections {
let currentSection = sections[section]
return currentSection.name
}
return nil
}
func numberOfSections(in tableView: UITableView) -> Int {
if let sections = fetchedResultsController.sections {
return sections.count
}
return 0
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//dispatch_async(dispatch_get_main_queue()) {
//[unowned self] in
print("didSelectRowAtIndexPath")//does not recognize first time pressed item for some reason?
let selectedCell = self.tableView(tableView, cellForRowAt: indexPath) as? InventoryTableViewCell
self.performSegue(withIdentifier: "inventoryInfoSegue", sender: selectedCell)
//}
}
#IBAction func BarcodeScanBarItemAction(sender: UIBarButtonItem) {
print("test of baritem")
}
#IBAction func SetStoreBarItemAction(sender: UIBarButtonItem) {
print("change store interface")
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.barcodeTextDidChange(searchText: searchText)
}
func barcodeTextDidChange(searchText: String){
print("text is changing")
//Code to change NSFetchRequest Here~ & Reload Table
m_searchText = searchText //sets the local variable to this class so the setFetchedResultsController() will update accordingly
//Perform another fetch again to get correct data~
do {
//fetchedResultsController. //this will force setter code to run again.
print("attempting fetch again, reset to use lazy init")
fetchedResultsController = setFetchedResultsController() //sets it again so its correct.
try fetchedResultsController.performFetch()
} catch {
print("An error occurred")
}
inventoryTable.reloadData()//refreshes the data~
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("ended by cancel")
searchBar.text = ""
m_searchText = "" //set the search text accordingly back to nothing.
//Perform another fetch again to get correct data~
do {
//fetchedResultsController. //this will force setter code to run again.
print("attempting fetch again, reset to use lazy init")
fetchedResultsController = setFetchedResultsController() //sets it again so its correct.
try fetchedResultsController.performFetch()
} catch {
print("An error occurred")
}
inventoryTable.reloadData()//refreshes the data~
searchBar.resignFirstResponder()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("ended by search")
searchBar.resignFirstResponder()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
print("ended by end editing")
searchBar.resignFirstResponder()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
print("DidBeginEditing")
//searchBar.keyboardType = UIKeyboardType.NamePhonePad
}
#IBAction func unwindBackToInventory(segue: UIStoryboardSegue) {
print("unwind attempt")
let barcode = (segue.source as? ScannerViewController)?.barcode
searchBar.text = barcode!
barcodeTextDidChange(searchText: searchBar.text!)//force it to re-run function manually.
print("barcode="+barcode!)
inventoryTable.reloadData()//reload the data to be safe.
}
}
//Extention to INT to create random number in range.
extension Int
{
static func random(range: ClosedRange<Int> ) -> Int
{
var offset = 0
if range.lowerBound < 0 // allow negative ranges
{
offset = abs(range.lowerBound)
}
let mini = UInt32(range.lowerBound + offset)
let maxi = UInt32(range.upperBound + offset)
return Int(mini + arc4random_uniform(maxi - mini)) - offset
}
}
globals.swift
import Foundation
import CoreData
//Array of Inventory & Store Core Data Managed Objects
var g_inventoryItems = [Inventory]()
var g_storeList = [Store]()
var g_appSettings = [AppSettings]()
var g_demoMode = false
Inventory+CoreDataProperties.swift
import Foundation
import CoreData
extension Inventory {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Inventory> {
return NSFetchRequest<Inventory>(entityName: "Inventory");
}
#NSManaged var addCount: NSNumber?
#NSManaged var barcode: String?
#NSManaged var barcodeReverse: String?
#NSManaged var barcodeFourth: String?
#NSManaged var currentCount: NSNumber?
#NSManaged var id: NSNumber?
#NSManaged var imageLargePath: String?
#NSManaged var imageSmallPath: String?
#NSManaged var name: String?
#NSManaged var negativeCount: NSNumber?
#NSManaged var newCount: NSNumber?
#NSManaged var store_id: NSNumber?
#NSManaged var store: Store?
//This is used for A,B,C ordering...
var lettersection: String {
let characters = name!.characters.map { String($0) }
return (characters.first?.uppercased())!
}
//This is used for 1,2,3 ordering... (using front of barcode and using barcodeReverse)
var numbersection: String {
let characters = barcode!.characters.map { String($0) }
return (characters.first?.uppercased())!
}
//This is used for 0000000123 ordering...(uses back number of barcode)
var numberendsection: String {
let characters = barcodeReverse!.characters.map { String($0) }
return (characters.first?.uppercased())!
}
//This is used for 0000000 -> 0123 ordering...(uses back 4th number of barcode)
var numberfourthsection: String {
let characters = barcodeFourth!.characters.map { String($0) }
//print("characters")
//print(characters)
return (characters.first?.uppercased())!
}
}
Inventory.Swift
import Foundation
import CoreData
class Inventory: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
Screenshots of Errors
I have reviewed your all comments and contents posted here.
You have not shared one file here, but the problem is occurring you are creating invalid managed objects in the context.
And then whenever you call viewWillAppear() function in InventoryViewController, it saves the context.
Finally, it synced empty records into your database.
During parsing those invalid objects, it tried to parse nil value, so crashed.
Please never set default value for managed objects you are defining as properties.
I hope this will clarify your issue.
I was running into similar issue and i moved to the new CoreData api introduced in ios10.
This uses the NSPersistentContainer class to create the stack and create associated contexts.
This eliminates the need to manually call save or order the creation of fetch results controller.
Good blog post to read: https://useyourloaf.com/blog/easier-core-data-setup-with-persistent-containers/
My setup is a follows
create a store NSPersistentContainer
let persistentContainer = NSPersistentContainer(name: "ModelFileName");
configure settings
let url = NSPersistentContainer.defaultDirectoryURL()
let path = url.appendingPathComponent(persistentContainer.name);
description.shouldAddStoreAsynchronously = true; //write to disk should happen on background thread
self.persistentContainer.persistentStoreDescriptions = [description];
load the store
persistentContainer.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
fatalError("Unresolved error \(error), \(error.localizedDescription)")
}
//configure context for main view to automatically merge changes
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true;
});
in the view controller you can access the view context by calling
persistentContainer.viewContext
if you need to make changes you can call
persistentContainer.performBackgroundTask({ (context) in ... });
or you can get a background context
let context = persistentContainer.newBackgroundContext()
context.perform({ ... })
In case this helps anyone else who gets the "API Misuse: Attempt to serialize store access on non-owning coordinator" error - I was getting the error because I accessed an object in a singleton that had not been destroyed and was still using the old NSManagedObjectContext after I reset the NSPersistentStore and NSManagedObjectContext.