I am working on an IOS project. I am trying to create Twitter. I am using Firebase Database. As you know Firebase uses JSON and i am confused about database creation.
First of all i have users with name, surname, email,username and profilepic URL.
Also i have posts which has a string named as post but as you know i need to show users in my post. A post object has its user's name, profilepic and username
And also users can follow another users and their timeline should have only posts sent by the users they follow.
This structure confuses me a lot here is a solution i have found and example of my json files
{
"users" : {
"1SUbzM6rIRTQexrOgJ8BnDBCDWt2" : {
"email" : "Test1#test.com",
"fullname" : "Test1 Test1",
"name" : "Test1",
"surname" : "Test1",
"username" : "#test1",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {}
},
"4vBvO9vURkPneusviRGxKglJ3n32" : {
"email" : "Test2#test.com",
"fullname" : "Test2 Test2",
"name" : "Test2",
"surname" : "Test2",
"username" : "#test2",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {
"34E20A52-8E66-4AF7-8DA4-73BDE9185FCB" : {
"post" : "Bir post daha\n"
},
"59798B81-4510-4E63-8050-3AF04698C7B0" : {
"post" : "3. Postumuz gör better approach"
}
},
"follows" : {
"5QaOU5Pd05h2M8wExcUteUg6mlJ2" : {
"email" : "TEST3#TEST.com",
"fullname" : "TEST3 TEST3",
"name" : "TEST3",
"surname" : "TEST3"
"username" : "#test3",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {}
},
"6y0RLGGCw6Zg5RHgPxghKUId9pJ3" : {
"email" : "TEST4#jjj.hhh",
"fullname" : "TEST4 TEST4",
"name" : "TEST4",
"surname" : "TEST4",
"username" : "#test4",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {}
},
},
},
"5QaOU5Pd05h2M8wExcUteUg6mlJ2" : {
"email" : "TEST3#TEST.com",
"fullname" : "TEST3 TEST3",
"name" : "TEST3",
"surname" : "TEST3"
"username" : "#test3",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {}
},
"6y0RLGGCw6Zg5RHgPxghKUId9pJ3" : {
"email" : "TEST4#jjj.hhh",
"fullname" : "TEST4 TEST4",
"name" : "TEST4",
"surname" : "TEST4"
"username" : "#test4",
"ppurl" : "www.ppurl.com/ppppp",
"posts" : {}
}
}
Is this a right approach? Users and posts duplicates and it is too hard to reach a data from the swift. Should i save posts different from users but if i do with this approach i cannot get post's username , name and ppurl etc.
How should i construct my JSON file and create relationships using the most efficient way.
Edit:
I can get my json files to my project. My question is : Is it true to write the same user 2-3 times ? I have a user already in my json file but when someone follows someone should i write it again inside of the friends attribute ? Or can i get reference with only id.
Why not use a simpler structure like this one:
{
"users": [
{
"identifier": "1SUbzM6rIRTQexrOgJ8BnDBCDWt2",
"email": "Test1#test.com",
"fullname": "Test1 Test1",
"name": "Test1",
"surname": "Test1",
"username": "#test1",
"ppurl": "www.ppurl.com/ppppp"
},
{
"identifier": "4vBvO9vURkPneusviRGxKglJ3n32",
"email": "Test2#test.com",
"fullname": "Test2 Test2",
"name": "Test2",
"surname": "Test2",
"username": "#test2",
"ppurl": "www.ppurl.com/ppppp",
"posts": [
"34E20A52-8E66-4AF7-8DA4-73BDE9185FCB",
"59798B81-4510-4E63-8050-3AF04698C7B0"
],
"follows": [
"5QaOU5Pd05h2M8wExcUteUg6mlJ2",
"6y0RLGGCw6Zg5RHgPxghKUId9pJ3"
]
},
{
"identifier": "5QaOU5Pd05h2M8wExcUteUg6mlJ2",
"email": "TEST3#TEST.com",
"fullname": "TEST3 TEST3",
"name": "TEST3",
"surname": "TEST3",
"username": "#test3",
"ppurl": "www.ppurl.com/ppppp"
},
{
"identifier": "6y0RLGGCw6Zg5RHgPxghKUId9pJ3",
"email": "TEST4#jjj.hhh",
"fullname": "TEST4 TEST4",
"name": "TEST4",
"surname": "TEST4",
"username": "#test4",
"ppurl": "www.ppurl.com/ppppp"
}
]
}
Otherwise you're JSON payload is going to grow bigger and bigger over time.
EDIT Just saw #vadian comment... Let's say it's based on his idea then.
Related
I am trying to create avro schema for below json
{
"id": "TEST",
"status": "status",
"timestamp": "2019-01-01T00:00:22-03:00",
"comment": "add comments or replace it with adSummary data",
"error": {
"code": "ER1212132",
"msg": "error message"
}
}
the error object is optional, it could be
"error" :{}
Below is the avro schema without default value
{
"type" : "record",
"name" : "Order",
"fields" : [ {
"name" : "id",
"type" : "string"
}, {
"name" : "status",
"type" : "string"
}, {
"name" : "timestamp",
"type" : "string"
}, {
"name" : "comment",
"type" : ["null","string"],
"default": null
}, {
"name" : "error",
"type" : {
"type" : "record",
"name" : "error",
"fields" : [ {
"name" : "code",
"type" : "string"
}, {
"name" : "msg",
"type" : "string"
} ]
}
} ]
}
How can I add default value {} for error field in json.
{
"type" : "record",
"name" : "Order",
"fields" : [ {
"name" : "id",
"type" : "string"
}, {
"name" : "status",
"type" : "string"
}, {
"name" : "timestamp",
"type" : "string"
}, {
"name" : "comment",
"type" : ["null","string"],
"default": null
}, {
"name" : "error",
"type" : [{"type": "record", "fields":[{"name": "code", "type":"string"}, {"name": "msg", "type":"string"}]}, {"type": "record", "fields":[]}]
} ]
}
I need to make a request body looking like this:
{
"accepted" : [
{
"deposit" : 2000,
"name" : "Tuxedo",
"rent" : 100,
"id" : 3,
"favourited_by" : [
],
"tag_id" : 21,
"status" : "unknown",
"image_url" : "https:\/\/www.moss.co.uk\/images\/extralarge\/965549415_01.jpg",
"addresses" : [
]
},
{
"deposit" : 3000,
"name" : "ps4",
"rent" : 50,
"id" : 2,
"favourited_by" : [
],
"tag_id" : 16,
"status" : "unknown",
"image_url" : "http:\/\/www.spokeslabs.com\/jstone\/ps4_images\/ps4-hrdware-large18.jpg",
"addresses" : [
]
},
{
"deposit" : 1000,
"name" : "Electric drill",
"rent" : 100,
"id" : 1,
"favourited_by" : [
],
"tag_id" : 11,
"status" : "unknown",
"image_url" : "https:\/\/static.independent.co.uk\/s3fs-public\/styles\/story_medium\/public\/thumbnails\/image\/2016\/06\/20\/12\/ryobi-rpd800-k-percussion-d.jpg",
"addresses" : [
]
}
],
"rejected" : [
],
"address" :
{
"city" : "Hong Kong",
"lng" : "114.162699999745",
"country" : "Hong Kong",
"street" : "Barker Road",
"id" : "0",
"label" : "Home",
"lat" : "22.269837686727"
}
}
Unfortunantly I'm sending this:
{
"accepted" : [
{
"deposit" : 2000,
"name" : "Tuxedo",
"rent" : 100,
"id" : 3,
"favourited_by" : [
],
"tag_id" : 21,
"status" : "unknown",
"image_url" : "https:\/\/www.moss.co.uk\/images\/extralarge\/965549415_01.jpg",
"addresses" : [
]
},
{
"deposit" : 3000,
"name" : "ps4",
"rent" : 50,
"id" : 2,
"favourited_by" : [
],
"tag_id" : 16,
"status" : "unknown",
"image_url" : "http:\/\/www.spokeslabs.com\/jstone\/ps4_images\/ps4-hrdware-large18.jpg",
"addresses" : [
]
},
{
"deposit" : 1000,
"name" : "Electric drill",
"rent" : 100,
"id" : 1,
"favourited_by" : [
],
"tag_id" : 11,
"status" : "unknown",
"image_url" : "https:\/\/static.independent.co.uk\/s3fs-public\/styles\/story_medium\/public\/thumbnails\/image\/2016\/06\/20\/12\/ryobi-rpd800-k-percussion-d.jpg",
"addresses" : [
]
}
],
"rejected" : [
],
"address" : [
{
"city" : "Hong Kong",
"lng" : "114.162699999745",
"country" : "Hong Kong",
"street" : "Barker Road",
"id" : "0",
"label" : "Home",
"lat" : "22.269837686727"
}
]
}
The difference is in the last section of the JSON. What I'm sending contains an array of addresses but I want to be sending just contains one address object.
This request body gets created the following way:
var parameters = [String:[AnyObject]]()
parameters["rejected"] = rejectedItemsArray as [AnyObject]
parameters["accepted"] = acceptedItemsArray as [AnyObject]
parameters["address"] = addressArray as [AnyObject]
let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
request.setValue(self.token!, forHTTPHeaderField: Constant.tokenUserDefaultsKey)
request.HTTPMethod = "POST"
do {
let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
let requestBodyString = String(data: data, encoding: NSUTF8StringEncoding)
NSLog("Request Body: %#", requestBodyString!)
request.HTTPBody = data
} catch ( _) {
NSLog("Failed to encode json for Post Items")
}
How do I fix it?
I want to have 2 arrays in my JSON:
1 - "accepted"
2 - "rejected"
And 1 single object in my JSON:
1 - "address"
Problem is in this line parameters["address"] = addressArray as [AnyObject]. You are setting Array with address key instead of that you need to set dictionary. So create addressDic like this.
let adddressDic = ["city" : "Hong Kong", "lng" : "114.162699999745", "country" : "Hong Kong",
"street" : "Barker Road", "id" : "0", "label" : "Home", "lat" : "22.269837686727"]
Now set this Dictionary with address key
parameters["address"] = adddressDic
Edit: You need to also change the declaration of parameters like this.
var parameters = [String:AnyObject]()
parameters["address"] = addressArray as [Any]
Here (in this line of code) you are assigning array instance. Create a JSON object (Dictionary) of 'addressArray' and then assign it to parameter["address"]
e.g.
var addresArray = [String : Any]()
//Store json values/information in it and then
parameters["address"] = addressArray as [String : Any]
I just learned about NSURLSession. I want to make POST request. it needs JSON as the body.
when using Objective-C I used NSDictionary, but in swift I Dictionary only hold 1 data type. what is the alternative to implement this in swift?
{
"userID" : "xxxxxxxx",
"full_name" : "xxxxxxxxx",
"pob" : "xxxxxxxxxx"
"dob" : "xxxxxxxxxxx",
"status" : 3,
"phone_number" : null,
"education" : [
{
"university" : "xxxxxx",
"location": {
latitude: "xxxxxxx",
longitude: "xxxxxxxx"
},
"degree": "xxxxxx"
},
{
"university" : "xxxxxx",
"location": {
latitude: "xxxxxxx",
longitude: "xxxxxxxx"
},
"degree": "xxxxxx"
}
]
}
i'm really stuck, I've looking for the solution for hours but no luck. haven't found any tutorial about this in google.
thank you very much, sorry for my bad English.
Just use Dictionary<Any, Any>, it's like NSDictionary
You can still use NSDictionary:
var dictionary: NSDictionary = [
"userID" : "xxxxxxxx",
"full_name" : "xxxxxxxxx",
"pob" : "xxxxxxxxxx",
"dob" : "xxxxxxxxxxx",
"status" : 3,
"phone_number" : NSNull(),
"education" : [
[
"university" : "xxxxxx",
"location": [
"latitude" : "xxxxxxx",
"longitude" : "xxxxxxxx"
],
"degree": "xxxxxx"
],
[
"university" : "xxxxxx",
"location": [
"latitude": "xxxxxxx",
"longitude": "xxxxxxxx"
],
"degree": "xxxxxx"
]
]
]
For easy working with JSON responses you can use libraries like:
https://github.com/owensd/json-swift
https://github.com/SwiftyJSON/SwiftyJSON
Try like this :-
var dictionary = Dictionary<String, Any>
dictionary["userID"] = "xxxxxxxx"
dictionary["status"] = 200
like that ...
I'm trying to grab the image of locations from Google Places. The JSON output is as follows, but I don't see how to access the image of each location. I want to put this image in a tablview with the listing for each place.
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.8719830,
"lng" : 151.1990860
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "677679492a58049a7eae079e0890897eb953d79b",
"name" : "Zaaffran Restaurant - BBQ and GRILL, Darling Harbour",
"rating" : 3.90,
"reference" : "CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc",
"types" : [ "restaurant", "food", "establishment" ],
"vicinity" : "Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney"
},
{
"geometry" : {
"location" : {
"lat" : -33.8722580,
"lng" : 151.1986550
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "27ea39c8fed1c0437069066b8dccf958a2d06f19",
"name" : "Criniti's Darling Harbour",
"rating" : 3.60,
"reference" : "CnRwAAAA-5kh5WZ3m1CMTO3LslvhRtAYsrOcQP7wB9AE1bV5R6Bd46NN5wB16MtImXWQ9eS1nWVbV_j-8iXYXRpU13Efp1t_d-Dp4WfEsFcYj-_g6db1SC1vAukyeCyotjS5xrwhzqWWAhgmA4qIliWeev2u1BIQprWzxl_hkj_w3QdTiUBYKxoUDVTjF4RugJdaJWkC4n6w6pSajKw",
"types" : [ "restaurant", "food", "establishment" ],
"vicinity" : "Shop 461, 2-10 Darling Drive, Harbourside Shopping Centre, DARLING HARBOUR"
},
...additional results...
],
"status" : "OK"
}
https://developers.google.com/maps/documentation/places/
The Google Places API does not currently support this feature. If you believe this would be a useful feature you can submit a 'Places API - Feature Request' here:
http://code.google.com/p/gmaps-api-issues/issues/entry?template=Places%20API%20-%20Feature%20Request
Suppose I’ve got the following schema:
{
"name" : "Profile",
"type" : "record",
"fields" : [
{ "name" : "firstName", "type" : "string" },
{ "name" : "address" , "type" : {
"type" : "record",
"name" : "AddressUSRecord",
"fields" : [
{ "name" : "address1" , "type" : "string" },
{ "name" : "address2" , "type" : "string" },
{ "name" : "city" , "type" : "string" },
{ "name" : "state" , "type" : "string" },
{ "name" : "zip" , "type" : "int" },
{ "name" : "zip4", "type": "int" }
]
}
}
]
}
I’m using a GenericRecord to represent each Profile that gets created. To add a firstName, it’s easy to do the following:
Schema sch = Schema.parse(schemaFile);
DataFileWriter<GenericRecord> fw = new DataFileWriter<GenericRecord>(new GenericDatumWriter<GenericRecord>()).create(sch, new File(outFile));
GenericRecord r = new GenericData.Record(sch);
r.put(“firstName”, “John”);
fw.append(r);
But how would I set the city, for example? How do I represent the key as a string that the r.put method can understand?
Thanks
For the schema above:
GenericRecord t = new GenericData.Record(sch.getField("address").schema());
t.put("city","beijing");
r.put("address",t);