Protobuf is a data transmission format developed by Google that uses pure binary data transmission and is much smaller than JSON.
Data transmission format
If you’re developing an APP, you’re going to have to read data from the server. The most popular way to do this is to use JSON as a data transfer format. The benefit of JSON is that the data structure is clear and readable. It’s much smaller than XML data.
It is these advantages that make JSON almost the standard format for data transfer today.
Let’s get down to business and talk about the protobuf we’re going to introduce this time. Why make a protobuf when JSON has so many advantages? Although the data volume of JSON is relatively small, its overall text structure is still plain text. That is to say, when transmitting data, the organization format of the data is also transmitted. Such as:
{
"name" : "swift",
"age" : 23
}Copy the code
Above is a simple JSON object. Although the format of the data is simple, it still transmits property names, such as name and age, as well as curly braces and quotes, which indicate the format of the data.
Of course, this isn’t a problem if you’re not so demanding of your APP’s Internet connection. But if one day you want to improve the transmission performance of your APP, then Protobuf is one of the solutions you can adopt.
protobuf
A protobuf is a Protocol Buffer. Protobuf’s main feature is binary transmission, and it only transmits “data”, not the “format” of the data. To use a protobuf, you first define the format of the data, using a file with the.proto extension:
syntax = "proto3";
message Person {
string name = 1;
int32 age = 2;
}Copy the code
This proto file should look familiar. The first line of syntax defines the syntax format for this file. Because protobuf has two main versions, 2 and 3, this specifies which version is used for the current file.
Next comes the definition of the message format. Obviously, we define the Person type with two attributes, name and age. String and int32, respectively.
After the protocol format is defined, use the protobuf command to convert the protocol file to an Objective-C class file:
$ protoc ./person.proto --objc_out ./Copy the code
The above command converts the person.proto file in the current directory to objC classes and prints it to the current directory. After successfully running the command, we should see two generated code files that we can add to the XCode project.
We can then use the Person class directly in our code to interact with the data:
Person *person = [[Person alloc] init];
person.name = @"swift";
person.age = 22;
NSData *dataWillSend = [person data];
Copy the code
As you can see, the Person class has exactly the same attributes as we defined in Person.proto, and it also provides a data method that serializes the data directly into NSData, so that when we send the request, we can send the NSData directly.
The Person class also provides methods to parse data if it is returned from a request received from the server:
[Person parseFromData:dataWillReceived error:nil];Copy the code
In this way, our data transmission is really only sending the data itself, not the data format. Because the format of the data and the parsing rules are kept locally on both the client and server.
conclusion
Protobuf provides us with just such a more efficient data transfer protocol. It has its advantages and disadvantages. The benefit is that we talked about earlier, maximizing the efficiency of data transmission. Compared to the JSON data format, it has a smaller data transfer volume. It only transmits the data itself, not the format information.
But again, it has some inconveniences, such as.proto data format files that must be saved on both the client and server sides. If the data format changes, it can be costly to adjust both sides simultaneously.
Overall, Protobuf gives us a new option and is a good solution if your APP is at a point where you need to fine-tune performance in a very detailed way. For more detailed and complete documentation on protobuf, you can refer to its Github page at github.com/google/prot…
If you find this article helpful, you can also follow the wechat official account Swift-cafe and I will share more of my original content with you