Firestore read document - swift

I am trying to read a document from Firestore but I face this problem
Error Domain=FIRFirestoreErrorDomain Code=14 "Failed to get document because the client is offline." UserInfo={NSLocalizedDescription=Failed to get document because the client is offline.}
Note : the collection and the document are existing
I am use Firebase/Firestore version 1.17.1
could anyone help me please?!

This is an example of getting data with Cloud Firestore (source: firebase.google.com)
Firstly create data:
let citiesRef = db.collection("cities")
citiesRef.document("SF").setData([
"name": "San Francisco",
"state": "CA",
"country": "USA",
"capital": false,
"population": 860000,
"regions": ["west_coast", "norcal"]
])
citiesRef.document("LA").setData([
"name": "Los Angeles",
"state": "CA",
"country": "USA",
"capital": false,
"population": 3900000,
"regions": ["west_coast", "socal"]
])
citiesRef.document("DC").setData([
"name": "Washington D.C.",
"country": "USA",
"capital": true,
"population": 680000,
"regions": ["east_coast"]
])
citiesRef.document("TOK").setData([
"name": "Tokyo",
"country": "Japan",
"capital": true,
"population": 9000000,
"regions": ["kanto", "honshu"]
])
citiesRef.document("BJ").setData([
"name": "Beijing",
"country": "China",
"capital": true,
"population": 21500000,
"regions": ["jingjinji", "hebei"]
])
Then the following example shows how to retrieve the contents of a single document using get():
let docRef = db.collection("cities").document("SF")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
This may be as well be an error if you are using firebase.firestore(), with firebase.database() it is working well. 😘

Related

How do I populate an embedded document attribute on mongoDB Atlas?

I've already successfully used an embedded document to create a nested amenities attribute, under a spot in my database, but now I'd like to populate it with true values. (by default they're false) The object that I created on mongoDB looks like this so far:
And on Postman, when I go to my spots index, this is what a spot looks like:
[
{
"amenities": {
"wifi": false,
"kitchen": false,
"breakfast": false,
"parking": false,
"pool": false,
"essentials": false
},
"_id": "5e4317871c9d440000b1613f",
"name": "Test home 1",
"latitude": 5.97,
"longitude": 62.54,
"city": "Los Angeles",
"state": "California",
"country": "United States",
"description": "This is a test description",
"occupancy": 4,
"bedrooms": 2,
"baths": 1,
"date": "2020-02-11T21:20:28.969Z"
}
]
How would I populate the amenities attribute on mongoDB? Do I need to add a line like this into the document on mongoDB?
amenities: { wifi: true, kitchen: true, breakfast: true, etc... }

Generic Sub-Document API in Couchbase

I want to update specific fields in below document stored in Couchbase :
"name": "Douglas Reynholm",
"email": "douglas#reynholmindustries.com",
"addresses": {
"billing": {
"line1": "123 Any Street",
"line2": "Anytown ",
"country": "United Kingdom"
},
"delivery": {
"line1": "123 Any Street",
"line2": "Anytown ",
"country": "United Kingdom"
}
},
"purchases": {
"complete": [
339, 976, 442, 666
],
"abandoned": [
157, 42, 999
]
}
}
How can I write a generic query using Java SDK to handle all possible combinations of subdocument update ? For example one user wants to update "addresses.delivery.country" field and other user wants to update "name" field.
It would be tough to right customized query for each possible case of subdocument update.. is that correct ?

How to Parse json data having array in IONIC hybrid appliaction

I am beginner in hybrid Ionic App Development. I want to implement RESTful web service in my project.
My json Data is:
{
"records": [
{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
},
{
"Name": "Ana Trujillo Emparedados y helados",
"City": "MĂ©xico D.F.",
"Country": "Mexico"
},
{
"Name": "Antonio Moreno TaquerĂ­a",
"City": "MĂ©xico D.F.",
"Country": "Mexico"
}]
}
Here i want to Parse this Data in Listview in ionic. I don't no how to parse data with array.Please suggest the solution or tutorials to get result.I want to show all Names in Listview.
I am using this api link: http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt
thanks in advance.
Here you no need to parse the JSON data, it is already in object format.
You can use like this
var myData = {
"records": [
{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
},
{
"Name": "Ana Trujillo Emparedados y helados",
"City": "MĂ©xico D.F.",
"Country": "Mexico"
},
{
"Name": "Antonio Moreno TaquerĂ­a",
"City": "MĂ©xico D.F.",
"Country": "Mexico"
}]
}
var record1 = myData.records[0];
var record2 = myData.records[1];
var record3 = myData.records[2];
console.log(record1)
console.log(record2)
console.log(record3)
console.log("Record 1 Data:")
console.log('\t'+"Name: "+record1.Name)
console.log('\t'+"City: "+record1.City)
console.log('\t'+"Country: "+record1.Country)
console.log("Record 2 Data:")
console.log('\t'+"Name: "+record2.Name)
console.log('\t'+"City: "+record2.City)
console.log('\t'+"Country: "+record2.Country)
console.log("Record 3 Data:")
console.log('\t'+"Name: "+record3.Name)
console.log('\t'+"City: "+record3.City)
console.log('\t'+"Country: "+record3.Country)

Convert .rtf file into NSDictionary or NSArray

I have a file (.rtf) that contains data that looks like:
[
{
"Symbol": "PIH",
"Name": "1347 Property Insurance Holdings, Inc.",
"LastSale": "6.8299",
"MarketCap": 41717882.9375,
"ADR TSO": "n/a",
"IPOyear": "2014",
"Sector": "Finance",
"Industry": "Property-Casualty Insurers",
"Summary Quote": "http://www.nasdaq.com/symbol/pih",
"FIELD10": ""
},
{
"Symbol": "FLWS",
"Name": "1-800 FLOWERS.COM, Inc.",
"LastSale": "9.27",
"MarketCap": 606557031.66,
"ADR TSO": "n/a",
"IPOyear": "1999",
"Sector": "Consumer Services",
"Industry": "Other Specialty Stores",
"Summary Quote": "http://www.nasdaq.com/symbol/flws",
"FIELD10": ""
},
{
"Symbol": "FCCY",
"Name": "1st Constitution Bancorp (NJ)",
"LastSale": "12.99",
"MarketCap": 103169203.98,
"ADR TSO": "n/a",
"IPOyear": "n/a",
"Sector": "Finance",
"Industry": "Savings Institutions",
"Summary Quote": "http://www.nasdaq.com/symbol/fccy",
"FIELD10": ""
}
]
Now, I would like to convert this data into something (NSDictionary, NSArray, etc.) that I can use to easily access the data. Any help would be appreciated.
guard let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt") else {
return nil
}
do {
let content = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
return content
} catch _ as NSError {
return nil
}
Save the .rtf file into a location (say desktop).
Read the file from that location

MongoDB Database Structure and Best Practices Help

I'm in the process of developing Route Tracking/Optimization software for my refuse collection company and would like some feedback on my current data structure/situation.
Here is a simplified version of my MongoDB structure:
Database: data
Collections:
“customers” - data collection containing all customer data.
[
{
"cust_id": "1001",
"name": "Customer 1",
"address": "123 Fake St",
"city": "Boston"
},
{
"cust_id": "1002",
"name": "Customer 2",
"address": "123 Real St",
"city": "Boston"
},
{
"cust_id": "1003",
"name": "Customer 3",
"address": "12 Elm St",
"city": "Boston"
},
{
"cust_id": "1004",
"name": "Customer 4",
"address": "16 Union St",
"city": "Boston"
},
{
"cust_id": "1005",
"name": "Customer 5",
"address": "13 Massachusetts Ave",
"city": "Boston"
}, { ... }, { ... }, ...
]
“trucks” - data collection containing all truck data.
[
{
"truckid": "21",
"type": "Refuse",
"year": "2011",
"make": "Mack",
"model": "TerraPro Cabover",
"body": "Mcneilus Rear Loader XC",
"capacity": "25 cubic yards"
},
{
"truckid": "22",
"type": "Refuse",
"year": "2009",
"make": "Mack",
"model": "TerraPro Cabover",
"body": "Mcneilus Rear Loader XC",
"capacity": "25 cubic yards"
},
{
"truckid": "12",
"type": "Dump",
"year": "2006",
"make": "Chevrolet",
"model": "C3500 HD",
"body": "Rugby Hydraulic Dump",
"capacity": "15 cubic yards"
}
]
“drivers” - data collection containing all driver data.
[
{
"driverid": "1234",
"name": "John Doe"
},
{
"driverid": "4321",
"name": "Jack Smith"
},
{
"driverid": "3421",
"name": "Don Johnson"
}
]
“route-lists” - data collection containing all predetermined route lists.
[
{
"route_name": "monday_1",
"day": "monday",
"truck": "21",
"stops": [
{
"cust_id": "1001"
},
{
"cust_id": "1010"
},
{
"cust_id": "1002"
}
]
},
{
"route_name": "friday_1",
"day": "friday",
"truck": "12",
"stops": [
{
"cust_id": "1003"
},
{
"cust_id": "1004"
},
{
"cust_id": "1012"
}
]
}
]
"routes" - data collections containing data for all active and completed routes.
[
{
"routeid": "1",
"route_name": "monday1",
"start_time": "04:31 AM",
"status": "active",
"stops": [
{
"customerid": "1001",
"status": "complete",
"start_time": "04:45 AM",
"finish_time": "04:48 AM",
"elapsed_time": "3"
},
{
"customerid": "1010",
"status": "complete",
"start_time": "04:50 AM",
"finish_time": "04:52 AM",
"elapsed_time": "2"
},
{
"customerid": "1002",
"status": "incomplete",
"start_time": "",
"finish_time": "",
"elapsed_time": ""
},
{
"customerid": "1005",
"status": "incomplete",
"start_time": "",
"finish_time": "",
"elapsed_time": ""
}
]
}
]
Here is the process thus far:
Each day drivers begin by Starting a New Route. Before starting a new route drivers must first input data:
driverid
date
truck
Once all data is entered correctly the Start a New Route will begin:
Create new object in collection “routes”
Query collection “route-lists” for “day” + “truck” match and return "stops"
Insert “route-lists” data into “routes” collection
As driver proceeds with his daily stops/tasks the “routes” collection will update accordingly.
On completion of all tasks the driver will then have the ability to Complete the Route Process by simply changing “status” field to “active” from “complete” in the "routes" collection.
That about sums it up. Any feedback, opinions, comments, links, optimization tactics are greatly appreciated.
Thanks in advance for your time.
You database schema looks like for me as 'classic' relational database schema. Mongodb good fit for data denormaliztion. I guess when you display routes you loading all related customers, driver, truck.
If you want make your system really fast you may embedd everything in route collection.
So i suggest following modifications of your schema:
customers - as-is
trucks - as-is
drivers - as-is
route-list:
Embedd data about customers inside stops instead of reference. Also embedd truck. In this case schema will be:
{
"route_name": "monday_1",
"day": "monday",
"truck": {
_id = 1,
// here will be all truck data
},
"stops": [{
"customer": {
_id = 1,
//here will be all customer data
}
}, {
"customer": {
_id = 2,
//here will be all customer data
}
}]
}
routes:
When driver starting new route copy route from route-list and in addition embedd driver information:
{
//copy all route-list data (just make new id for the current route and leave reference to routes-list. In this case you will able to sync route with route-list.)
"_id": "1",
route_list_id: 1,
"start_time": "04:31 AM",
"status": "active",
driver: {
//embedd all driver data here
},
"stops": [{
"customer": {
//all customer data
},
"status": "complete",
"start_time": "04:45 AM",
"finish_time": "04:48 AM",
"elapsed_time": "3"
}]
}
I guess you asking yourself what do if driver, customer or other denormalized data changed in main collection. Yeah, you need update all denormalized data within other collections. You will probably need update billions of documents (depends on your system size) and it's okay. You can do it async if it will take much time.
What benfits in above data structure?
Each document contains all data that you may need to display in your application. So, for instance, you no need load related customers, driver, truck when you need display routes.
You can make any difficult queries to your database. For example in your schema you can build query that will return all routes thats contains stops in stop of customer with name = "Bill" (you need load customer by name first, get id, and look by customer id in your current schema).
Probably you asking yourself that your data can be unsynchronized in some cases, but to solve this you just need build a few unit test to ensure that you update your denormolized data correctly.
Hope above will help you to see the world from not relational side, from document database point of view.