core data with duplicate entity values in swift - ios

I am parsing some data from Json and saved into the coredata,after fetching core data showing into the tableview working fine, tableview is show all the value in repeatedly how can avoid the repeated values I tried many ways but not find way
Json format
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi#gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp#gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio#gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
when I fetching "name" showing repeated values
these are save and fetch code
func getfetchdata()
{
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Contacts")
do{
let fetchResults = try self.context.fetch(fetchRequest) as? [Contacts]
if fetchResults!.count > 0 {
for bEntity in fetchResults! {
let employeeEntity = bEntity
print(employeeEntity.name as Any)
TableviewData.append(ContactsDataVal.init(name: employeeEntity.name!,
id: employeeEntity.id!, email: employeeEntity.email!, gender: employeeEntity.gender!, address: employeeEntity.address!))
}
print("data values already")
}}
catch let error as NSError
{
print(error)
}
}
func getdata()
{
let url = URL(string: "https://api.androidhive.info/contacts/")
URLSession.shared.dataTask(with: url!) { (Data, response, error) in
do
{
let data = try JSONSerialization.jsonObject(with: Data!, options: JSONSerialization.ReadingOptions.allowFragments)as! [String:AnyObject]
let arraydata = data["contacts"]as! [[String:Any]]
for arravalues in arraydata
{
let entityDescription = NSEntityDescription.entity(forEntityName: "Contacts", in:self.context)
let favouriteObj = Contacts(entity: entityDescription!, insertInto: self.context)
favouriteObj.name = arravalues["name"] as? String
favouriteObj.id = arravalues["id"] as? String
favouriteObj.email = arravalues["email"] as? String
favouriteObj.gender = arravalues["gender"] as? String
favouriteObj.address = arravalues["address"] as? String
do {
try self.context.save()
}
}
}catch let error as NSError{
print("error",error)
}
}
.resume()
}
how to avoid repeated values in core data and show proper data into the tableview

First of all in getdata do not save the context in each iteration of the loop, save it once after the loop.
To avoid duplicates fetch all contacts from Core Data, map them to the names and check if the array contains the received name
func getdata()
{
let url = URL(string: "https://api.androidhive.info/contacts/")
let names : [String]
do {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Contacts")
let fetchResults = try self.context.fetch(fetchRequest)
names = fetchResults.map{ $0.name }
} catch {
names = []
}
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil { print(error!); return }
do {
let data = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
let arraydata = data["contacts"] as! [[String:Any]]
for arravalues in arraydata
{
guard let name = arravalues["name"] as? String, !names.contains(name) else { continue }
let entityDescription = NSEntityDescription.entity(forEntityName: "Contacts", in:self.context)
let favouriteObj = Contacts(entity: entityDescription!, insertInto: self.context)
favouriteObj.name = name
favouriteObj.id = arravalues["id"] as? String
favouriteObj.email = arravalues["email"] as? String
favouriteObj.gender = arravalues["gender"] as? String
favouriteObj.address = arravalues["address"] as? String
}
try self.context.save()
} catch {
print("error",error)
}
}
.resume()
}
Notes:
A Core Data fetch with generic fetch request returns always a non-optional array of the NSManagedObject subclass which is specified as generic type on success.
Never check for empty array with foo.count > 0, use !foo.isEmpty
A JSON dictionary in Swift 3+ is always [String:Any] rather than [String:AnyObject]
Handle a potential error in the dataTask completion block.
Name the first parameter in the completion block lowercased (data) to avoid a confusion with the type Data.
Omit the options parameter in jsonObject(with as the result is clearly a collection type.

Maybe there is something that can be done to the fetch request similar to How to get distinct results from a single field in Core Data (Swift 4) but another option would be to remove the duplicates by simply creating a set from the fetch result:
let fetchSet = Set(fetchResults)
and iterate over the set instead

Related

Fetch and Check internal (Local) JSON file in swift

I want to check city name From local JSON if found then massage show "You got it". But one problem came occurred that is I fetch data from file is successfully but I'd know how to compare it.
Here is my JSON file look like:
{
"data": [
{
"zip_code": 55001,
"latitude": 44.90717,
"longitude": -92.816193,
"city": "Afton",
"state": "MN",
"county": "Washington"
},
{
"zip_code": 55002,
"latitude": 45.513447,
"longitude": -92.894239,
"city": "Almelund",
"state": "MN",
"county": "Chisago"
}
]
}
Code is here:
func FatchingInformation(){
do {
if let file = Bundle.main.url(forResource: "Zone", withExtension: "json") {
let data = try Data(contentsOf: file)
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let object = json as? [String: Any] {
// This condition work and Get JSON on Debug Area
print("Obj is ::: \(object)")
} else if let object = json as? [Any] {
// json is an array
print("Object is \(object)")
} else {
print("JSON is invalid")
}
} else {
print("no file")
}
} catch {
print(error.localizedDescription)
}
}
you are in right way on your JSON is dict of array of dictionary, you need to iterate your [String: Any] first there after check it contains array or dict then you need to follow as below
if let object = json as? [String: Any], let objectArray = object["data"] as? [[String: Any]] {
// do stuff
for getDictItems in objectArray{
if let getCityCompare = getDictItems["city"] as? String, !getCityCompare.isEmpty, getCityCompare == "Almelund"{
print("city name is \(getCityCompare)")
break
}
}
}
You ca use decodable Struct to decode json
// MARK: - Address
struct Address: Codable {
let data: [Datum]
}
// MARK: - Datum
struct Datum: Codable {
let zipCode: Int
let latitude, longitude: Double
let city, state, county: String
enum CodingKeys: String, CodingKey {
case zipCode = "zip_code"
case latitude, longitude, city, state, county
}
}
let address = try? JSONDecoder().decode(Address.self, from: jsonData)

Error trying to parse JSON using URLSession in swift

I'm trying to parse a test JSON from a http adress, but I get an error saying that
"No value associated with key CodingKeys(stringValue: \"name\", intValue: nil)
The JSON looks like this. It has been validated, so it should work ok:
{
"work": [
{
"name": "Jocke",
"job": "Developer",
"date": "1985-12-30T00:00:00+0000",
"best_book": {
"id": 33245,
"title": "DiscWorld",
"author": {
"id": 345,
"name": "Terry Prattchet"
}
}
},
{
"name": "Bob",
"job": "Construction worker",
"date": "2010-01-30T00:00:00+0000",
"best_book": {
"id": 375802,
"title": "Ender's Game (Ender's Saga, #1)",
"author": {
"id": 589,
"name": "Orson Scott Card"
}
}
}
]
}
The code looks like this:
struct People: Codable {
let name: String
let job: String
enum OuterKey: String, CodingKey {
case work = "work"
}
enum codingKeys: String, CodingKey {
case name = "name"
case job = "job"
}
init(decoder: Decoder) throws {
let outerContainer = try decoder.container(keyedBy: OuterKey.self)
let innerContainer = try outerContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .work)
self.name = try innerContainer.decode(String.self, forKey: .name)
self.job = try innerContainer.decode(String.self, forKey: .job)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://api.myjson.com/bins/fe2eo") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
let decodedJson = try! jsonDecoder.decode([People].self, from: data)
}
}.resume()
}
}
I'm just trying to grasp the first two keys as of now, just to see if it works. But it doesn't even get past name.
Your api returns
[{"firstName":"Jocke","job":"developer"},{"firstName":"Anna","job":"construction"},{"firstName":"Peter","job":"pilot"}]
do {
let jsonDecoder = JSONDecoder()
let decodedJson = try jsonDecoder.decode([People].self, from: data)
}
catch {
print(error)
}
struct People: Codable {
let firstName, job: String
}
just try this
struct Work:Codeable {
let work:[People]
}
struct People: Codable {
let name: String
let job: String
}
do {
let jsonDecoder = JSONDecoder()
let decodedJson = try jsonDecoder.decode(Work.self, from: data)
}
catch {
print(error)
}
if you have same name as json keys you don't need to use codingkeys

How can I decode JSON with array and more JSON inside?

I recently began with swift. I need decode the json on below.
The JSON has inside two more JSON the first one (validation) does not matter. The second one (result) has a JSON array inside (serviceCenter). I need the information of each servicenter. I try to use servicecenter as decodeable class to get a servicenter object, but as the JSON does not have the proper format I can't do it.
[
{
"validation": {
"bValid": true,
"sDescription": "Access true."
}
},
{
"result": {
"serviceCenter": [
{
"model": "Vanquish",
"color": "Purple",
"make": "Aston Martin",
"sTag": "3666",
"sVin": "6JDO2345",
"sMiles": "3666",
"bDamage": "1",
"dDateTime": "04-17-2018 9:38 AM"
},
{
"model": "F360",
"color": "Red",
"make": "Ferrari",
"sTag": "0010",
"sVin": "6JDO2347",
"sMiles": "80000",
"bDamage": "1",
"dDateTime": "04-17-2018 9:25 AM"
},
{
"model": "Vanquish",
"color": "Purple",
"make": "Aston Martin",
"sTag": "0009",
"sVin": "6JDO2345",
"sMiles": "25000",
"bDamage": "1",
"dDateTime": "04-17-2018 9:23 AM"
},
{
"model": "Vanquish",
"color": "Purple",
"make": "Aston Martin",
"sTag": "0003",
"sVin": "6JDO2345",
"sMiles": "20000",
"bDamage": "1",
"dDateTime": "04-12-2018 10:37 AM"
}
]
}
}
]
I try so much but i cant do it.
This its my code now, Could someone help me please
do {
let parseoDatos = try JSONSerialization.jsonObject(with: data!) as! [AnyObject]
let h = type(of: parseoDatos )
print("'\(parseoDatos)' of type '\(h)'")
let contenido = parseoDatos[1]["result"]
if let services = contenido!! as? Dictionary<String, Array<Any>> {
for (_,serviceArray) in services {
for sc in serviceArray{
let h = type(of: sc )
print("'\(sc)' of type '\(h)'")
}
}
}
} catch {
print("json processing failed")
}
the result of print sc is
{
bDamage = 1;
color = Purple;
dDateTime = "04-17-2018 9:38 AM";
make = "Aston Martin";
model = Vanquish;
sMiles = 3666;
sTag = 3666;
sVin = 6JDO2345;
}' of type '__NSDictionaryI'
You can use Codable
Initial response have array of InitialElement and InitialElement is is struct with validation , result , result may be nil
don't forget to add your URL at url
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if let initial = try? JSONDecoder().decode([InitialElement].self, from: data){
// inital now have array of InitialElement and InitialElement is is struct with validation , result , result may be nil
print(initial)
}
}.resume()
With this Model for Data:
import Foundation
struct InitialElement: Codable {
let validation: Validation?
let result: ResultData?
}
struct ResultData: Codable {
let serviceCenter: [ServiceCenter]
}
struct ServiceCenter: Codable {
let model, color, make, sTag: String
let sVin, sMiles, bDamage, dDateTime: String
}
struct Validation: Codable {
let bValid: Bool
let sDescription: String
}
extension InitialElement {
init(data: Data) throws {
self = try JSONDecoder().decode(InitialElement.self, from: data)
}
}
try this code
enum ParsingError: Error {
case wrongFormat(String)
}
do {
let jsonObject = try JSONSerialization.jsonObject(with: data!)
guard let array = jsonObject as? [Any] else {
throw ParsingError.wrongFormat("wrong root object")
}
guard array.count == 2 else {
throw ParsingError.wrongFormat("array count != 2")
}
guard let dict = array[1] as? [String: Any] else {
throw ParsingError.wrongFormat("can't parse dict from array")
}
guard let serviceCenters = (dict["result"] as? [String: Any])?["serviceCenter"] else {
throw ParsingError.wrongFormat("can't parse serviceCenters")
}
guard let serviceCentersArray = serviceCenters as? [[String : Any]] else {
throw ParsingError.wrongFormat("serviceCenters is not an array")
}
print("\(type(of: serviceCentersArray))\n", serviceCentersArray)
} catch {
print("json processing failed: \(error)")
}
Thanks all for yours answers, it's my first question here, and i feels great all the help. Finally i can resolve the problem. Maybe not the best way, but here its the code:
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil{
print("error=\(String(describing: error))")
return
}
do {
let parseoDatos = try JSONSerialization.jsonObject(with: data!) as! [AnyObject]
let h = type(of: parseoDatos )
print("'\(parseoDatos)' of type '\(h)'")
let contenido = parseoDatos[1]["result"]
if let services = contenido!! as? Dictionary<String, Array<Any>> {
for (_,serviceArray) in services {
for sc in serviceArray{
let h = type(of: sc )
print("'\(sc)' of type '\(h)'")
let valid = JSONSerialization.isValidJSONObject(sc) // true
print(valid)
do {
let jsonData = try JSONSerialization.data(withJSONObject: sc, options: .prettyPrinted)
let serviceDecoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
if let scJSON = serviceDecoded as? [String:String] {
print ("--------------------------")
print("'\(scJSON)' of type '\(type(of: scJSON))'")
print ("--------------------------")
}
} catch {
print(error.localizedDescription)
}
i think later y try to use Codable as suggested, but for now the code is working ok. Thanks again!
//try this it is working
let arrayMain=try?JSONSerialization.jsonObject(with:jsonData!,options:.mutableLeaves) as! Array<Any>
//print(arrayMain!)
if let firstDictionart=arrayMain![0] as? [String: Any] {
if let insideFirstDict = firstDictionart["validation"] as? [String: Any]{
print(insideFirstDict["bValid"]!)
print( insideFirstDict["sDescription"]!)
}
}
if let resultDictionary=arrayMain![1] as? [String: Any] {
if let serviceDictionary = resultDictionary["result"] as? [String: Any] {
for array in serviceDictionary["serviceCenter"] as! Array<Any>{
if let infoDicti=array as? [String: Any] {
print( infoDicti["color"]!)
print( infoDicti["make"]!)
print( infoDicti["color"]!)
print( infoDicti["sTag"]!)
print( infoDicti["sVin"]!)
print( infoDicti["sMiles"]!)
print( infoDicti["sVin"]!)
print( infoDicti["model"]!)
print( infoDicti["bDamage"]!)
print( infoDicti["dDateTime"]!)
}
}
}
}

parsing two different object's array from JSON response

I'm trying to parse JSON data .. i know how to do it for one object array .. but i don't know how to do it for a response which has to two arrays of different objects ..
for example this is the JSON:
{
"shifts": [
{
"id": 4,
"region": "Eastren",
"city": "Khobar",
"nationality": "1",
"id_service": 2,
"shift_date": "2018-04-05",
"shift_type": "night",
"weekday": "sunday",
"quantity_staff": 8,
"lead_hours": 2,
"created_at": "2018-04-23 11:46:20",
"updated_at": "2018-04-24 08:46:14",
"deleted_at": null
},
{
"id": 5,
"region": "Eastren",
"city": "Khobar",
"nationality": "Phili",
"id_service": 2,
"shift_date": "2018-04-04",
"shift_type": "night",
"weekday": "sunday",
"quantity_staff": 8,
"lead_hours": 2,
"created_at": "2018-04-23 11:47:25",
"updated_at": "2018-04-23 12:53:05",
"deleted_at": "2018-04-23"
}
],
"prices": [
{
"id": 1,
"id_service": 2,
"nationality": "Phili",
"price": 150,
"created_at": "2018-04-23 11:43:40",
"updated_at": "2018-04-23 11:43:40",
"deleted_at": null
}
]
}
it has two array of objects .. shifts and prices .. how to parse each one of them?
my function:
func GetShiftsAndPrices(id: Int){
let todosEndpoint: String = "my link"
guard let todosURL = URL(string: todosEndpoint) else {
print("Error: cannot create URL")
return
}
var todosUrlRequest = URLRequest(url: todosURL)
todosUrlRequest.httpMethod = "POST"
todosUrlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
let newTodo: [String: Any] = ["id_service": id]
let jsonTodo: Data
do {
jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
todosUrlRequest.httpBody = jsonTodo
} catch {
print("Error: cannot create JSON from todo")
return
}
let session = URLSession.shared
let task = session.dataTask(with: todosUrlRequest) {
(data, response, error) in
guard error == nil else {
print("error calling POST on /public/api/login_customer")
print(error!)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
//WHAT SHOULD I DO HERE?
print("Success!")
} catch {
print("error parsing response from POST")
return
}
}
task.resume()}
}
I have classes for both shift and price .. and know how to get each one if it was alone in a response .. like:
shifts:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let shifts1 = try decoder.decode([Shifts].self, from: responseData)
var shifts = [Shift]()
for shift in shifts1{
let newshift = Shift(id: shift.id, region: shift.region, city: shift.city, nationality: shift.nationality, idService: shift.idService, shiftDate: shift.shiftDate, shiftType: shift.shiftType, weekday: shift.weekday, quantityStaff: shift.quantityStaff, leadHours: shift.leadHours)
shifts.append(newshift)
}
let userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: shifts)
userDefaults.set(encodedData, forKey: "shifts")
userDefaults.synchronize()
let decoded = userDefaults.object(forKey: "shifts") as! Data
let decodedShift = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Shift]
for shift in decodedShift {
print(shift.id)
}
prices:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let prices1 = try decoder.decode([Prices].self, from: responseData)
print("1")
var prices = [Price]()
for price in prices1{
let newprice = Price(id: price.id, idService: price.idService,nationality: price.nationality, price: price.price)
print("2")
prices.append(newprice)
print(newprice.nationality)
}
let userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: prices)
userDefaults.set(encodedData, forKey: "prices")
userDefaults.synchronize()
let decoded = userDefaults.object(forKey: "prices") as! Data
let decodedPrice = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Price]
for price in decodedPrice {
print(price.nationality)
}
how to parse them both in one JSON response.. I'm new to this.. can someone please tell me how to do it?
Use an umbrella struct
struct Root : Decodable {
let shifts : [Shift]
let prices : [Price]
}
and two different structs for shifts and prices:
struct Shift : Decodable {
let id: Int
let region, city, nationality : String
let idService : Int
let shiftDate, shiftType, weekday : String
let quantityStaff, leadHours : Int
let createdAt, updatedAt : String
let deletedAt : String?
}
struct Price : Decodable {
let id, idService : Int
let nationality : String
let price : Int
let createdAt, updatedAt : String
let deletedAt : String?
}
To decode the JSON write
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder .decode(Root.self, from: responseData)
print(result.prices)
print(result.shifts)
} catch { print(error) }
Further I recommend to decode keys like shiftType and weekday directly into an enum for example
enum ShiftType : String, Decodable {
case day, night
}
struct Shift : Decodable {
let id: Int
...
let shiftType : ShiftType
...
let shifts = responseJson.valueFor(key:"shifts") as? [String] ?? []
let prices = responseJson.valueFor(key:"prices") as? [String] ?? []
print("\(shifts) and \(prices)")

Swift and JSON parsing only an object not an array

I am working on a weather app that parses JSON data and sets the text of my label to the temp value of the JSON request. I got the value of id from the weather object array, but the temp is not in an array it is just an object. Can someone please tell me where I am wrong. My value is reurning nil because I am not fetching it correctly. Here is my snippet and JSON.
#IBAction func getWeather(sender: AnyObject) {
let requestURL: NSURL = NSURL(string: "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=MYAPPID")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("JSON Downloaded Sucessfully.")
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
if let today = json["weather"] as? [[String: AnyObject]] {
//this is pulling 4 key value pairs
for weather in today {
//this works
let id = weather["id"]?.stringValue
self.trumpDescription.text=id;
print(id)
//this is where I am confused it changes from an array to just an object
let temp = json["temp"] as? String
self.currentTempView.text=temp;
print(temp)
}
}
}
catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
}`
Here is the JSON:
{
"coord": {
"lon": 138.93,
"lat": 34.97
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}
],
"base": "cmc stations",
"main": {
"temp": 292.581,
"pressure": 1019.48,
"humidity": 99,
"temp_min": 292.581,
"temp_max": 292.581,
"sea_level": 1028.92,
"grnd_level": 1019.48
},
"wind": {
"speed": 5.36,
"deg": 237.505
},
"clouds": {
"all": 64
},
"dt": 1464964606,
"sys": {
"message": 0.0037,
"country": "JP",
"sunrise": 1464895855,
"sunset": 1464947666
},
"id": 1851632,
"name": "Shuzenji",
"cod": 200
}
It looks like it should be
if let main = json["main"] as? NSDictionary {
let temp = main["temp"] as! String
print(temp)
}
Instead of this:
let temp = json["temp"] as? String
Try this:
if let main = json["main"] as? [String: AnyObject] {
let temp = main[temp]?.stringValue
print(temp)
//alternatively you can try this as per your convenience of data type
let tempNew = main[temp]?.doubleValue
print(tempNew)
}

Resources