{"__v":8,"_id":"5735d38cea59092200825540","category":{"project":"54774d9af3736008009e9e0e","version":"56a0bd9e3697d80d002ac5e7","_id":"5735a52431a73b1700887ca0","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-05-13T09:57:56.274Z","from_sync":false,"order":13,"slug":"data-objects","title":"Data Objects"},"parentDoc":null,"project":"54774d9af3736008009e9e0e","user":"54774c47f3736008009e9e0c","version":{"__v":20,"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","createdAt":"2016-01-21T11:14:38.131Z","releaseDate":"2016-01-21T11:14:38.131Z","categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Sockets","version_clean":"1.1.0","version":"1.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-05-13T13:15:56.961Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Overview\"\n}\n[/block]\nAs you already know, Data Objects can have various custom field types (read about it [here](classes#section-types-of-class-schema-fields)). One of the possible types is `integer`. If you choose to add this field you can not only set the field value but also increment or decrement it by a certain amount (for example `1` or `5`).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"How does it work?\"\n}\n[/block]\nLet's say we have a game app and we created a Data Class named `Player`. It holds information about game users, and has one attribute called `number_of_games`. Players can buy games inside your app (we will be increasing `number_of_games` value) and every time they play a game - we want to decrease value of `number_of_games` by 1. We could do it by updating it's value - e.g. when the old value was 20, we could update it with 19. But to avoid conflicts, for situations where game is played at the same time on two devices, it's much safer to user our increase/decrease api.\n\nInstead of sending a new value of the field when a player buys 10 games, you could send the following: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{“number_of_games”: {“_increment”: 10}}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nWhen player played 1 game and we want to decrease it by one, we could send:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{“number_of_games”: {“_increment”: -1}} \",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nThis way, you don't even have to know the old value of the field!\n\nThe whole request could look like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X PATCH \\\\\\n-H \\\"X-API-KEY: API_KEY\\\" \\\\\\n-H \\\"Content-type: application/json\\\" \\\\\\n-d '{\\\"number_of_games\\\": {\\\"_increment\\\":10}}' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/INSTANCE_NAME/classes/player/objects/data_object_id/\\\"\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"import syncano\\nfrom syncano.models import Object\\n\\nconnection = syncano.connect(api_key='API_KEY')\\n\\nObject.please.increment(\\n  'counter', #field to increment\\n  1, #by what value to increment\\n  instance_name='instance_name, \\n  class_name='class_name', \\n  id=object_id #e.g. id = 45 \\n  different_string_field = 'new content' #you can also update different fields \\n)\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"var Syncano = require(\\\"syncano\\\");  // CommonJS\\nvar connection = Syncano({accountKey: \\\"ACCOUNT_KEY\\\"});\\nvar DataObject = connection.DataObject;\\n\\nvar query = {\\n\\tid: 7,\\n  instanceName: \\\"INSTANCE_NAME\\\",\\n  className: \\\"CLASS_name\\\"\\n};\\n\\nvar book = {\\n  number_of_games: {_increment: -1}\\n};\\n\\nDataObject.please().update(query, book).then(function(book) {\\n\\tconsole.log(\\\"book\\\", book)\\n});\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \"Book book = new Book();\\n// object to update has to have id set\\nbook.setId(serverId);\\nbook.increment(\\\"number_of_games\\\", 10);\\nbook.save();\",\n      \"language\": \"java\",\n      \"name\": \"Android\"\n    },\n    {\n      \"code\": \"[[Book please] giveMeDataObjectsWithCompletion:^(NSArray * _Nullable objects, NSError * _Nullable error) {\\n    Book *book = (Book *)[objects firstObject];\\n    [book incrementKey::::at:::\\\"numOfPages\\\" by:@12 withCompletion:^(NSError * _Nullable error) {\\n        //Handle error\\n    }];\\n}];\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"Book.please().giveMeDataObjects { (books, error) in\\n  let book:Book = books?.first as! Book\\n  book.incrementKey(\\\"numOfPages\\\", by: 12, withCompletion: { (error) in\\n      //Handle error\\n  })\\n}\",\n      \"language\": \"swift\"\n    },\n    {\n      \"code\": \"// NOT AVAILABLE\",\n      \"language\": \"csharp\",\n      \"name\": \"Unity\"\n    }\n  ]\n}\n[/block]","excerpt":"In this chapter you will learn how to increment and decrement integer fields in your Data Objects","slug":"incrementing-decrementing-fields","type":"basic","title":"Incrementing / decrementing fields"}

Incrementing / decrementing fields

In this chapter you will learn how to increment and decrement integer fields in your Data Objects

[block:api-header] { "type": "basic", "title": "Overview" } [/block] As you already know, Data Objects can have various custom field types (read about it [here](classes#section-types-of-class-schema-fields)). One of the possible types is `integer`. If you choose to add this field you can not only set the field value but also increment or decrement it by a certain amount (for example `1` or `5`). [block:api-header] { "type": "basic", "title": "How does it work?" } [/block] Let's say we have a game app and we created a Data Class named `Player`. It holds information about game users, and has one attribute called `number_of_games`. Players can buy games inside your app (we will be increasing `number_of_games` value) and every time they play a game - we want to decrease value of `number_of_games` by 1. We could do it by updating it's value - e.g. when the old value was 20, we could update it with 19. But to avoid conflicts, for situations where game is played at the same time on two devices, it's much safer to user our increase/decrease api. Instead of sending a new value of the field when a player buys 10 games, you could send the following: [block:code] { "codes": [ { "code": "{“number_of_games”: {“_increment”: 10}}", "language": "json" } ] } [/block] When player played 1 game and we want to decrease it by one, we could send: [block:code] { "codes": [ { "code": "{“number_of_games”: {“_increment”: -1}} ", "language": "json" } ] } [/block] This way, you don't even have to know the old value of the field! The whole request could look like this: [block:code] { "codes": [ { "code": "curl -X PATCH \\\n-H \"X-API-KEY: API_KEY\" \\\n-H \"Content-type: application/json\" \\\n-d '{\"number_of_games\": {\"_increment\":10}}' \\\n\"https://api.syncano.io/v1.1/instances/INSTANCE_NAME/classes/player/objects/data_object_id/\"", "language": "curl" }, { "code": "import syncano\nfrom syncano.models import Object\n\nconnection = syncano.connect(api_key='API_KEY')\n\nObject.please.increment(\n 'counter', #field to increment\n 1, #by what value to increment\n instance_name='instance_name, \n class_name='class_name', \n id=object_id #e.g. id = 45 \n different_string_field = 'new content' #you can also update different fields \n)", "language": "python" }, { "code": "var Syncano = require(\"syncano\"); // CommonJS\nvar connection = Syncano({accountKey: \"ACCOUNT_KEY\"});\nvar DataObject = connection.DataObject;\n\nvar query = {\n\tid: 7,\n instanceName: \"INSTANCE_NAME\",\n className: \"CLASS_name\"\n};\n\nvar book = {\n number_of_games: {_increment: -1}\n};\n\nDataObject.please().update(query, book).then(function(book) {\n\tconsole.log(\"book\", book)\n});", "language": "javascript" }, { "code": "Book book = new Book();\n// object to update has to have id set\nbook.setId(serverId);\nbook.increment(\"number_of_games\", 10);\nbook.save();", "language": "java", "name": "Android" }, { "code": "[[Book please] giveMeDataObjectsWithCompletion:^(NSArray * _Nullable objects, NSError * _Nullable error) {\n Book *book = (Book *)[objects firstObject];\n [book incrementKey:@\"numOfPages\" by:@12 withCompletion:^(NSError * _Nullable error) {\n //Handle error\n }];\n}];", "language": "objectivec" }, { "code": "Book.please().giveMeDataObjects { (books, error) in\n let book:Book = books?.first as! Book\n book.incrementKey(\"numOfPages\", by: 12, withCompletion: { (error) in\n //Handle error\n })\n}", "language": "swift" }, { "code": "// NOT AVAILABLE", "language": "csharp", "name": "Unity" } ] } [/block]