How to organise data when populating UICollectionView with images using Firebase - ios

I'm new to Swift, and coding in general, and have been working on a project where I'd like to create a UICollectionView populated with images from Firebase.
Each section of the UICollectionView would be a category, and each category would contain images related to that category. Each UICollectionView belongs to a parent, and I need to keep track of which parent has which categories, and which images are in each category.
To track the parents, categories, and images, I've set up the Firebase database in the following way (with bowl being the parent, the names of fruit as categories, and the keys are references to image data stored elsewhere in the database):
"bowl" : {
"apple" : {
"-LOM1R4EH9nszjJp0Va5" : true,
"-LOM1aRZT2XCE-6fvLBK" : true,
"-LOM1hSTmRY6wGrWMvIo" : true,
"-LOM1xnvKE6lc7fizomh" : true
},
"banana" : {
"-LOLmQWLXXyiCUwDBwID" : true
},
"pear" : {
"-LOLHakW-EtqevCeHfzl" : true,
"-LOM2DBGGuX5VQLmBz46" : true
},
"orange" : {
"-LOM26_pm6lbJ1D6hVPB" : true
}
}
The image data section of the database looks as follows:
"image" : {
"fruit" : {
"-LOLHakW-EtqevCeHfzl" : {
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
},
"-LOM1R4EH9nszjJp0Va5" : {
"description" : "small apple",
"imageURL" : "https://firebasestorage.googleapis.com/1/image3"
}
}
}
The approach I have been attempting to take is to create a dictionary with the image keys in it, then iterate through the image keys to grab the image data associated with each key (such as the imageURL), and then use the imageURL to download the images and populate the UICollectionView.
I've created a struct, as follows to transform the image data:
struct FruitPicture {
let imageURL: String
let description: String
init(imageURL: String, description: String) {
self.imageURL = imageURL
self.description = description
}
init?(snapshot: DataSnapshot) {
guard
let value = snapshot.value as? [String: AnyObject],
let imageURL = value["imageURL"] as? String,
let description = value["description"] as? String else {
return nil
}
self.imageURL = imageURL
self.description = description
}
func toAnyObject() -> Any {
return [
"imageURL": imageURL,
"description": description
]
}
}
I've been able to gather the imageURLs and populate a UICollectionView but it doesn't include the category details, and so far has involved a lot of manipulation of the data via snapshots, dictionaries, arrays, arrays of dictionaries, and so on, from one configuration to another and back again, and I've now become stuck and confused.
I've started looking at using multiple structs and nesting one within the other, like so, but I'm muddled on it all and am spending hours getting nowhere:
struct Picture {
var url: URL
var image: UIImage?
}
struct PictureCategory {
var name: String
var pictures: [Picture]
}
I was hoping for some advice, or roadmap, or details of how you would approach this, or some sample code, or anything to point me in the right direction. Thanks.
Edit to add more info
Thank you Iraniya your reply was very helpful and helped me consider things in a different way, I really appreciate it.
Taking your advice I've written the following which looks up a bowling creates a snapshot of the image meta data within (e.g the fruit and keys associated with that fruit) then uses those keys to create a snapshot of the image data (e.g key, imageURL, description). I then transform both snapshots into dictionaries, and return the dictionaries to the method which called it:
// GET DATA
static func getPicData(forKey bowlKey: String, completion: #escaping ([String : [Any]], [String : [FruitPicture]]) -> Void) {
var imageMetaDict: [String : [Any]] = [:]
var imageDataDict: [String : [FruitPicture]] = [:]
// DEFINE DATABASE TARGET
let ref = Database.database().reference().child("meta").child("bowl").child(bowlKey).child("fruit")
// GET DATA INTO SNAPSHOT AND TRANSFORM INTO DICTIONARY
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dict = snapshot.value as? [String:[String:Any]] else {
return completion([:],[:])
}
// DEFINE DISPATCH GROUP
let dispatchGroup = DispatchGroup()
// ITERATAE THROUGH DICTIONARY
for (categoryObject, fruitData) in dict {
// CREATE ARRAY TO STORE ITEMS
var itemArray = [String]()
// ITERATE THROUGH ITEMS IN FRUIT DATA
for item in fruitData {
// APPEND ITEM.KEY TO ITEM ARRAY
itemArray.append(item.key)
// ENTER DISPATCH GROUP
dispatchGroup.enter()
// USE ITEM.KEY TO GATHER IMAGE DATA
Service.viewPicData(forKey: item.key) { (fruitItem) in
if let fruitItem = fruitItem {
imageDataDict[item.key] = [fruitItem]
}
// EXIT DISPATCH GROUP
dispatchGroup.leave()
}
}
// STORE ARRAY IN DICTIONARY UNDER FRUIT CATEGORY KEY
imageMetaDict[categoryObject] = itemArray
}
// RETURN COMPLETION
dispatchGroup.notify(queue: .main, execute: {
completion(imageMetaDict, imageDataDict)
})
})
}
Each dictionary looks similar to the following:
imageMetaDict
[
"apple": ["-LOM1R4EH9nszjJp0Va5", "-LOM1xnvKE6lc7fizomh", "-LOM1hSTmRY6wGrWMvIo", "-LOM1aRZT2XCE-6fvLBK"],
"pear": ["-LOLHakW-EtqevCeHfzl", "-LOM2DBGGuX5VQLmBz46"],
"banana": ["-LOLmQWLXXyiCUwDBwID"],
"orange": ["-LOM26_pm6lbJ1D6hVPB"]
]
imageDataDict
[
"-LOM26_pm6lbJ1D6hVPB": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image1", description: "pear 1")],
"-LOM2DBGGuX5VQLmBz46": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image2", description: "banana 1")],
"-LOLmQWLXXyiCUwDBwID": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image3", description: "apple 1")]
]
Is this on the right track with what you were suggesting?
From what I understand the next steps are:
Create an array of fruit.keys sorted alphabetically
Use the fruit.keys to get image.keys from 'imageMetaDict'
Use those image.keys to look up the image data (imageURL, etc) in 'imageDataDict'
Transform all of this data into a new FruitDict which contains fruitCategory -> [fruitObject]
Is this similar to what you were suggesting? I'm happy to hear any further pointers, code or suggestions you have, you've really helped me so far!

To store images Create imageDict hash-map(dictionary) with the key you getting from firebase key in image->>fruits eg: "-LOLHakW-EtqevCeHfzl" with value you are getting or the stuct you already create, now when populating fruits-->apple get key from your bowl array or dict and then use that same key to get the image from imagesDict dict(hashmap you just create earlier
now while storing data in firebase make sure each image has unique keys and store that same key in your bowl-->apple->image that way it will be fast and easy to manage as image data and fruits data are mapped using key you get while storing new image :-) if you like the solution I can explain in more dept :-) #HappyCoding
Example
firebase node
"bowl" : {
"apple" : {
"-LOLHakW-EtqevCeHfzl" : true,
"--LOLmQWLXXyiCUwDBwID" : false,
}
}
"image" : {
"fruit" : {
"-LOLHakW-EtqevCeHfzl" : {
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
}
}
}
ImageDict
"-LOLHakW-EtqevCeHfzl":{
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
}
or
{"-LOLHakW-EtqevCeHfzl":imageStruct1,
"-LOLmQWLXXyiCUwDBwID" :imageStruct2}
now to show image while populating apple
var keys = boul["apple"].allKeys;
if(boul["apple"][keys[0]]){ //value is true show image
var imageUrl = imageDict[keys[0]["imageURL"]; //if using dict
//or
var image = imageDict[key[0]].imageURL //if using struct
}
Now to store parent, categories and there image details
create a Dict called fruitsDict or whatever with dict in side of another dict making key as fruite name eg: "apple":{apples Details like image price etc} but if you only interested in storing images just create list of images which have true value eg: "apple":[key1, key2...]; (keys you get from imageDict.
Now based on your requirement like
Show all category!! then create all category from fruitDict use that as datasource
and use imageDict and fruitsDict for details
Show only specific category like based on seasonal fruits then crate list of those fruits and show those based on imageDict and fruiteDict
HappyCoding :-)

Related

Save Nested Dictionary in UserDefaults and manage the duplication check [Swift 4.2]

I have a nested Dictionary required to save in UserDefaults and share to extension. The dictionary structure like below:
let dict = [
"Sections" : [
["Title" : "Title1", "Items": ["item1-1", "item1-2", "item1-3"]],
["Title" : "Title2", "Items": ["item2-1", "item2-2", "item2-3", "item2-4"]],
["Title" : "Title3", "Items": ["item3-1"]],
]
]
Which saved successfully with:
UserDefaults(suiteName: "group.identifier.test")!.setValue(dict, forKey: "savedDict")
But now I wish to get it back and check is Title2 already exists, if yes then delete it and add again with new Items
I used to do following but can't get the Title back:
let savedDict:[String:AnyObject] = UserDefaults(suiteName: "group.identifier.test")!.object(forKey: "savedDict") as! Dictionary
success to get the data under "Sections" by following code
let savedSection = savedDict["Sections"]
print("Saved Section: \(savedSection)")
but not able to get the Title with:
print("Saved Title: \(savedSection!["Title"])") *// return nil*
I tried for (key, value) too, but fired a data type error
for (key, value) in savedSection{ *// Type 'AnyObject?' does not conform to protocol 'Sequence'*
print("Key: \(key) Value: \(value)")
}
May I know is there any way to get the "Title" back for checking and update? Am I using the wrong way to store this kind of nested data?
Many Thanks!
in your code
print("Saved Title: \(savedSection!["Title"])") *// return nil*
here it should be
if let savedSection = savedDict["Sections"] as? [[String : Any]] { //EDIT***
print("Saved Title: \(savedSection[0]["Title"])") *// inplace of 0 any index you want,
}
as if now in your dictionary there are three elements in section so it safe to get value at 0, hope you understood that the underlying dictionary is array of dictionary in sections key, also instead of using dictionary you can use struct or class to save your data and while getting it retrieve it as that struct type.
First of all, never use KVC method setValue(:forKey with UserDefaults.
There is generic set(:forKey. And there is dictionary(forKey: to get a [String:Any] dictionary back
The value for key Sections is an array (index-based). Lets assume you have this new data
let newTitle2 : [String:Any] = ["Title" : "Title2", "Items": ["item4-1", "item4-2", "item4-3"]]
This is a way to load the dictionary – you should always safely check if the dictionary exists – update it and save it back. If the item for "Title2" exists it will be overwritten otherwise the new item is appended to the array.
let groupDefaults = UserDefaults(suiteName: "group.identifier.test")!
if var savedDict = groupDefaults.dictionary(forKey: "savedDict"),
var sections = savedDict["Sections"] as? [[String:Any]] {
if let title2Index = sections.firstIndex(where: {($0["Title"] as! String) == "Title2"}) {
sections[title2Index] = newTitle2
} else {
sections.append(newTitle2)
}
savedDict["Sections"] = sections
groupDefaults.set(savedDict, forKey: "savedDict")
}

Taking out array from dictionary in swift 3

Hi I am trying to populate a view using the response obtained from service but not able to fetch the exact value out of the whole response ,
[
["product_id": PRO161519,
"name": clothes,
"brand_name": Levis,
"discountprice": 0,
"images": <__NSArrayM 0x6000002541c0>(
{
image = "HTTP://i.vinove.com/dnn/backend/uploads/954tshirt_PNG5434.png";
}
)
"category": Accessories,
"price": 23.00
]
]
ProductList-Model
import UIKit
import SpeedLog
let KImages = "images"
let KListImage = "image"
struct ProductList{
var images = ""
var itemArray = [String]()
func bindProductListDataToPopulateView(_ response:[[String:Any]])->[ProductList]{
SpeedLog.print("response value as result",response)
for items in response{
print("items values",items)
}
print("item array",itemArray)
return []
}
}
response value as result
[["image":
item Values
["image":
Kindly help me to get the values images here.
You have to use like this :
for product in products {
if let productImages = product["images"], let images = productImages as? NSArray {
for image in images {
if let image = image as? [String: String] {
print(image["image"])
}
}
}
}
More than likely that JSON response you posted will eventually find its way to you in the form of a key-value Dictionary. You then use a "key" from the JSON you posted to extract the key's corresponding "value". In the snippet you posted, the keys would be the values on the left of the colon (e.g. "product_id", "name", etc).
Now, lets say your dictionary of key-values was called "jsonDictionary". You then would extract the values like so:
let productId = jsonDictionary["product_id"]
let name = jsonDictionary["name"]
If, however, you don't have logic to deserialize that raw JSON data (that you posted in your question) into a Dictionary, then you'll have to start there instead.

JSQMessageData append Json data

I am trying to add json values in JSQMessageData to show the message on JSQMessagesViewController. The view is set up and this the lite chat(can chat only once).We use an api to send and receive messages. The problem is when I fetched data from api as json it returns the value. I want to append that json data to the rest of my JSQMessages objects, I tried the last few days and have failed to accomplish this. Here is the full code and json response.
APIHandler.requestGETURL(urlString, success: { (JSON) in
print(JSON)
// var messageDictionary : [JSQMessageData] = []
// this is the message object
// i want to add the json data to my messageDictionary
// reload collection view
/*
{
"message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "hi",
"user_thumb" : "<image_path>"
},
{
"message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "how are you?",
"user_thumb" : "<image_path>"
}
*/
// i want to
let arrayNames = JSON["data"]
self.messageDictionary.append(JSQMessageData())
// I am stuck here
}) { (Error) in
print(Error.localizedDescription)
}
If I understand you correctly you're trying to parse json into a JSQMessage object. Your message data is not overly complex, it contains all the things a standard JSQMessage needs. So there is not any reason to create your own JSQMessageData object. You can just use one of the JSQMessage initializers. Since you are only using "Text" messages and not any other "Media" the
JSQMessage(senderId: <String!>, senderDisplayName: <String!>, date: <#Date>, text: <String>)
should be all you need. So all you need to do is get the values out of your json response. There are many ways to do this.
I am going to assume that the json you provided is also wrapped in a list like this
[
{ "message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "hi",
"user_thumb" : "<image_path>"
},
{ "message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "how are you?",
"user_thumb" : "<image_path>"
}
]
We can utilise the flatmap function to get our "Messages" out of the json data. You also do not need a dictionary becasue there is not key for each message so just use a list that contains JSQMessageObjects
var messages:[JSQMessages] = []
var imageDictionary: [userID: String: imagePath: String] = [:]
APIHandler.requestGETURL(urlString, success: { (JSON) in
print(JSON)
let messagesJSON = response.result.value as? [[String: Any]] ?? [[:]]
guard let listOfMessages = JSON as? [[String: AnyObject]]
messages: [JSQMessage] = listOfMessages.flatmap { messageData in
guard let dateCreated = messageData["message_time"] as? Date,
let userID = messageData["user_id"] as? String,
let userName = messageData["user_name"] as? String,
let text = messageData["message"] as? String else {
//If any of these things are missing we do not want to save the entry
return nil
}
let imagePath = messageData["user_thumb"] as? String
imageDictionary[userID] = imagePath
return JSQMessage(senderId: userID, senderDisplayName: userName, date: dateCreated, text: text)
}) { (let error: Error) in
if error != nil {
print(Error.localizedDescription)
}
}
I would save your image paths into a dictionary and fetch them on a background thread that way users can view the messages while the images populate as they arrive. Then once they have loaded apply them to your messages. or you can add it to your own custom message object that conformes to the JSQMessageDataSource protocol. for more on how to accomplish that check out this post
#Daniel is saying right, your json is enough simple that you don't need to add any JSQMessageData and maybe you are actually doing some extra effort, i have faced similar kind of problem when i need to pass a NSDictionary object with JSQMessage Objects so i used a tricky way for doing that ( and it works perfectly fine :) )
not sure about your case but this helps me a lot in many situations so follow these steps :
convert your json data into string
now save this json string into the accessebilityHint property of JSQmessage object. like -
(jsqmessageObj).accessibilityHint = jsonString
as according to your need as you want to use this json just extract the JSQmessage Object (like in cellForRowAtIndexPath ! ) , just use (jsqmessageObj).accessibilityHint to get back your json string decode it and use it as your need.
like - strJson = (jsqmessageObj).accessibilityHint
Hope this will help :p

Matching an exact key in SwiftyJson

I'm really having trouble with determining a specific value returned in SwiftyJson; hopefully, someone can help explain this to me.
I want to see if there is a match between a predetermined word, "apple" to any of the words received from the JSON responses.
If there is a match then a message is displayed, and the user either chooses to progress to the next level or the user returns to the home screen.
If there is no match then a message is displayed, and the user must either continue playing or cancel playing.
I would like to do this for multiple words across different levels of the game.
Level one: match "apple" to any of the received JSON responses.
Level two: match "computer" to any of the received JSON responses.
Level three: match "telephone" or "phone" or "iPhone" or "Android" or any or all of the above to any of the received JSON responses.
So, basically, I can get all of the JSON responses, but I'm having a hard time finding out how to set up to determine if there is a specific, predefined JSON response returned.
I have looked everywhere for weeks with another post but to no avail :(
JSON RESPONSES
{
"responses" : [
{
"labelAnnotations" : [
{
"mid" : "\/m\/01m2v",
"score" : 0.9245476,
"description" : "computer keyboard"
},
{
"mid" : "\/m\/01c648",
"score" : 0.7945268,
"description" : "laptop"
},
{
"mid" : "\/m\/01mfj",
"score" : 0.74227184,
"description" : "computer hardware"
},
{
"mid" : "\/m\/0541p",
"score" : 0.7062791,
"description" : "multimedia"
},
{
"mid" : "\/m\/07c1v",
"score" : 0.7039645,
"description" : "technology"
},
{
"mid" : "\/m\/03gq5hm",
"score" : 0.69323385,
"description" : "font"
},
{
"mid" : "\/m\/0bs7_0t",
"score" : 0.6724673,
"description" : "electronic device"
},
{
"mid" : "\/m\/01vdm0",
"score" : 0.66489816,
"description" : "electronic keyboard"
},
{
"mid" : "\/m\/0121tl",
"score" : 0.60392517,
"description" : "electronic instrument"
},
{
"mid" : "\/m\/0h8n5_7",
"score" : 0.5834592,
"description" : "laptop replacement keyboard"
}
]
}
]
}
CODE TO SHOW ALL JSON RESPONSES
// Use SwiftyJSON to parse results
let json = JSON(data: dataToParse)
let errorObj: JSON = json["error"]
// Parse the response
print(json)
let responses: JSON = json["responses"][0]
// Get label annotations
let labelAnnotations: JSON = responses["labelAnnotations"]
let numLabels: Int = labelAnnotations.count
var labels: Array<String> = []
if numLabels > 0 {
var labelResultsText:String = "Labels found: "
for index in 0..<numLabels {
let label = labelAnnotations[index]["description"].stringValue
labels.append(label)
}
for label in labels {
// if it's not the last item add a comma
if labels[labels.count - 1] != label {
labelResultsText += "\(label), "
} else {
labelResultsText += "\(label)"
}
}
self.labelResults.text = labelResultsText
} else {
self.labelResults.text = "No labels found"
}
EDIT
I'm apparently not able to answer my own question, I'll post an edit since I think it's a better solution but #pierce's was pretty decent for a single word, not many; it just wasn't applicable for a game setting application.
So, I created a new NSObject, created a
static var _words: [[String]] = [
["apple", "computer", "beer"]]
then
func checkAnnotations(annotations: [Annotation]) -> Bool {
var isMatched = false
let searchWords = self.words
for searchWord in searchWords {
for annotation in annotations {
if searchWord == annotation.descriptionString {
isMatched = true
break
}
}
if isMatched {
break
}
}
return isMatched
}
then created a function to handle the level state,
and finally compared that to the JSON response in the View Controller and advanced level if matched
// Get JSON key value
let labelAnnotations = responses["labelAnnotations"].arrayValue
let annotationObjects: [Annotation] = labelAnnotations.flatMap({ annotationDictionary in
if let mid = annotationDictionary["mid"].string,
let score = annotationDictionary["score"].double,
let description = annotationDictionary["description"].string {
let annotation = Annotation(mid: mid, score: score, descriptionString: description)
return annotation
}
return nil
})
//print(annotationObjects)
let searchString = LevelState.shared.words[0]
print("Level \(LevelState.shared.level), looking for: \(searchString)")
var isMatched = LevelState.shared.checkAnnotations(annotations: annotationObjects)
if isMatched {
LevelState.shared.advance()
}
let alertTitle = isMatched ? "Congrats! You got \(searchString)" : "Keep looking for \(searchString)"
//let translationResult = "Translated: \(levelDescription) to \(translatedText)"
let alertController = UIAlertController(title: alertTitle, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
self.prepareForNewLevel()
})
}
First thing - I really don't understand why you want to append a comma at the end of each description. I really thing that's unnecessary. Are you confused about separating elements in an array? Because the actual Strings don't require that, it's only if you're manually writing out the elements of an array (i.e. let array = ["a", "b", "c"]).
So then say you setup a property for this labels array, which is an array of Strings.
var labels: Array<String> = []
Once you've gone through and appended all the description values from your JSON, you can then manipulate it.
if numLabels > 0 {
for index in 0..<numLabels {
let label = labelAnnotations[index]["description"].stringValue
labels.append(label)
}
}
Now you could create a method that would return a filtered array of Strings based on some user entered word:
func findMatches(_ userEntry: String) -> [String] {
return labels.filter { $0.contains(userEntry) }
}
Now you could use the above method to handle some sort of user entry, like say you had the text from a UITextField named textField:
// Return the filtered matches based on the textField text (unless nil)
let matches = findMatches(textField.text ?? "")
// Print the number of matches, and also show the matches
print("Found \(matches.count) matches to user input\r\(matches)")
Now if you had labels which held ["a", "aa", "ba", "b", "c", "apple"], and ran the above code where the userEntry was just the letter "a", you'd see this print out in the console window:
Found 4 matches to user input
["a", "aa", "ba", "apple"]
EDIT - You can use the findMatches method above for what you're trying to do with pre-determined words to match. I'm not sure what you're trying to do exactly, but there are a couple different ways. First, say you had an array of pre-determined words you wanted to check as an array:
let words = ["word", "verb", "noun", "adverb"]
Then you could loop through that and check each one
for word in words {
let matches = findMatches(word)
if matches.count > 0 {
print("Found \(matches.count) matches to \(word)\r\(matches)")
} else {
// Do whatever you want when there are no matches
print("No matches found")
}
}
If you want to just check for a specific word, and have a specific response you could setup a method like so:
func checkWord(word: String, noMatchResponse: String) {
let matches = findMatches(word)
if matches.count > 0 {
print("Found \(matches.count) matches to \(word)\r\(matches)")
} else {
// Do whatever with the no match response
print(noMatchResponse)
}
}
There are so many ways you could implement this. You could also use a switch statement, and then use different case statements for each pre-determined word. It's really up to you and how you want to design your game.

Insert values into related entity in CoreData

I have two Entities in CoreData Model (Products and Images), there is a one-to-many relationship between products(one)-images(many). I have also subclassed my entities, and i am trying to populate the database from json file; however i cant seem to understand how to insert images into Images that are related to Products, so that when i pick that product in the app i get all the related images.
extension Products {
#NSManaged var price: NSNumber?
#NSManaged var stock: NSNumber?
#NSManaged var desc: String?
#NSManaged var name: String?
#NSManaged var images: NSSet?
#NSManaged var sales: NSSet?
}
extension Images {
#NSManaged var image: String?
#NSManaged var products: Products?
}
Then the json file data (which i dont have any problem serialising it with NSJSONObjectWithData:
{ "records" : [{
"name" : "Apple iMac 27in",
"description" : "The stunning all-in-one iMac features a beautiful 27-inch Retina 5K widescreen display, quad-core Intel Core i5 processors and AMD Radeon R9 M380 graphics processor with 2GB of GDDR5 memory.",
"image" : ["imac1.jpg", "imac2.jpg"],
"stock" : 32,
"price" : 1699.00
},
{
"name" : "Apple iPhone 6s Plus 128 GB",
"description" : "The moment you use iPhone 6s Plus, you know you’ve never felt anything like it. With just a single press, 3D Touch lets you do more than ever before. Live Photos bring your memories to life in a powerfully vivid way. And that’s just the beginning. Take a deeper look at iPhone 6s Plus, and you’ll find innovation on every level.",
"image" : ["iphone6splus1.jpg", "iphone6splus2.jpg", "iphone6splus3.jpg", "iphone6splus4.jpg"],
"stock" : 144,
"price" : 1042.00
}
]}
this is the function which i use to populate the database in the AppDelegate just after checking that the database is empty.
func fillDatabase() {
let jsonURL = NSBundle.mainBundle().URLForResource("products", withExtension: "json");
let data = NSData(contentsOfURL: jsonURL!);
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! Dictionary<String, AnyObject>;
let recordset = jsonData["records"] as! Array<Dictionary<String, AnyObject>>;
for record in recordset {
let product = Products(context: managedObjectContext);
if let name = record["name"] {
product.name = name as? String;
}
if let stock = record["stock"] as? NSNumber {
product.stock = stock;
}
if let price = record["price"] as? Double {
product.price = price;
}
if let desc = record["description"] as? String {
product.desc = desc;
}
if let images = record["image"] as? NSMutableArray {
// *********************************************************
// Im getting the array of images, but dont know where to go from here.
}
do {
try managedObjectContext.save();
} catch let error as NSError {
print(error.debugDescription);
}
}
} catch let error as NSError {
print("error parsing json object: \(error.debugDescription)");
return;
}
}
Given that you set up the relationships correctly in Core Data, you should be able to associate the images by using something like this:
product.images = NSSet(array: imageArray)
Make sure that the imageArray variable contains objects of type Images.
So to summarize, first you need to parse the array and create Images products from that array:
var imageArray = [Images]()
for yourRawImageFromJSON in json{
let image1 = Images(context: managedObjectContext)
// set properties on image
yourImages.append(image1)
}
then, once you have that array of Images, do as I said above (adding it here once again for the sake of completeness):
product.images = NSSet(array: imageArray)
Side note
You should follow the general rule that classes are named in a singular format, so instead of using Images and Products as your class names, you should be using Image and Product. Even though a class represents a generalised concept (much like in a database system, where conceptual classes are named plurally), it's more common and more convenient to use a singular form (for example it's more concise to use let product = Product() as opposed to let product = Products() because the latter conveys the notion that you are instantiating multiple instances, even though that's clearly not the case)
I'm not using CoreData like you do here in my project, but if the other parts of your code are working as intended, this should do the job.
if let images = record["image"] as? NSMutableArray {
for i in 0..<images.count {
let imageObject = Images(context: managedObjectContext)
imageObject.image = images[i]
let images = product.mutableSetValueForKey("images")
images.addObject(imageObject)
}
}
And about the naming of your CoreData objects I agree with the #the_critic, you actually have image objects and product objects. And your product objects has multiple image objects. Which means making the name of the object "image" and name of the relationship "images" in your data model could be much better.
Is not a good practice to store Arrays in core data, Therefore, this is how I would do it:
First: you need to create an Image Object.
extension Images { /* Images+Methods */
class func save(images: [NSDictionary]) -> [Images]? {
let imagesArray = [Images]()
for imageObject in images {
// creates a new Images NSManagedObject
guard let imageEntity = NSEntityDescription.insertNewObjectForEntityForName("Images", inManagedObjectContext: managedObjectContext) as? Images else {
return nil
}
if let image = imageObject["image"] as? String {
imageEntity.image = image
imagesArray.append(imageEntity)
}
}
return imagesArray
}
Then, call the Save(images: [NSDictionary]) method from your previous method.
if let images = record["image"] as? NSMutableArray {
// after what you already have, add the following line
if let images = record["images"] as? NSMutableArray, let imagesObjectArray = Images.save(images) {
product.images = NSSet(array:imagesObjectArray)
}
}
This should do it. Because is creating an Images NSManagedObject for every image that you have.
Hope this helps
P.S
You really need to change your classes name.

Resources