The author of this article: Ding Tongzhou, from the technical team of Kingdee Suishouji.
1 Introduction
This article continues the previous article " Kingdee Sui Notes Team Sharing: Still Using JSON? Protobuf Makes Data Transmission Cheaper and Faster (Principle) ", taking the Objective-C code on the iOS side as an example, to show you how to use Protobuf.
study Exchange:
- Instant messaging development exchange group: 320837163 [Recommended]
- Introductory article on mobile IM development: " One entry is enough for beginners: developing mobile IM from scratch "
(This article is simultaneously published at: http://www.52im.net/thread-1515-1-1.html )
2. Series of articles
This article is the second in a series of articles, the general directory is as follows:
" Kingdee Handy Notes Team Sharing: Still Using JSON? Protobuf Makes Data Transmission Cheaper and Faster (Practical Chapter) " (this article)
In addition, if you still intend to systematically understand the development knowledge of IM, you can read "A Beginner's Guide: Developing Mobile IM from Scratch ".
3. References
Detailed explanation of Protobuf communication protocol: code demonstration, detailed principle introduction, etc.
" A Java Code Demonstration Based on Protocol Buffer "
" How to choose the data transmission format of instant messaging applications "
" Strongly recommend Protobuf as your instant messaging application data transfer format "
" All-round evaluation: Is Protobuf performance 5 times faster than JSON? 》
" Technical Problems to be Faced in Mobile IM Development (Including Communication Protocol Selection) "
" Combining theory with practice: a detailed explanation of a typical IM communication protocol design "
" Detailed explanation of how to use Google's Protobuf in NodeJS "
4. Basic introduction
Protocol buffers is a cross-platform, multi-language supported and open source serialized data format proposed by Google. Compared to similar XML and JSON, Protocol buffers are smaller, faster and simpler. Its syntax is currently divided into proto2 and proto3 formats.
At present, the latest release version of Google's official Protobuf is 3.5.1, and the following are all based on this version of the environment.
For the use of Protocol Buffer, you can refer to the official documentation: https://developers.google.com/protocol-buffers/docs/overview .
5. Preparation
5.1 Environmental Requirements
Objective-C 2.0 Runtime (32bit & 64bit iOS, 64bit OS X)
Xcode 7.0+
Notice:
Protobuf does not use ARC for performance reasons, but it can be used under ARC.
5.2 Installation
Download the Protobuf code package ( https://github.com/google/protobuf/releases ), here select protobuf-objectivec-3.5.1.tar.gz.
5.3 Unzip the code package
To compile Protobuf, you may need to install some tools here:
$ brew install autoconf
$ brew install automake
$ brew install libtool
Run the following script to compile:
$ ./autogen.sh
$ ./configure
$ make
$ makeinstall
Check if protobuf is installed successfully:
$ protoc --version
If the version number is printed successfully, the installation is successful:
libprotoc 3.5.1
6. Using Protobuf in iOS
6.1 Create .proto file
Here Person.proto is created using a sample data structure from the official documentation:
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enumPhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phone = 4;
}
Use the command line to compile Person.proto as an objective-c file, and the compiled files are Person.pbobjc.h and Person.pbobjc.m:
protoc Person.proto --objc_out=./
6.2 Introducing Protobuf runtime resources
Google's official documentation provides two introduction methods, but the compilation fails when the first one is used, so the second one is chosen here:
Copy the protobuf directory: objectivec/*.h, objectivec/google/protobuf/*.pbobjc.h, objectivec/google/protobuf/*.pbobjc.m, and objectivec/*.m after removing objectivec/GPBProtocolBuffers.m .
Use the command line directly here, first enter the objectivec directory under protobuf:
$ cdprotobuf-3.5.1/objectivec
Then copy the files that meet the rules to the specified project directory:
$mkdir~/ProtobufDemo/ProtocolBuffers~/ProtobufDemo/ProtocolBuffers/google~/ProtobufDemo/ProtocolBuffers/google/protobuf
$ cp*.h *.m ~/ProtobufDemo/ProtocolBuffers
$ cpgoogle/protobuf/*.pbobjc.h google/protobuf/*.pbobjc.m ~/ProtobufDemo/ProtocolBuffers/google/protobuf
Notice:
The above command does not exclude the GPBProtocolBuffers.m file, it needs to be manually excluded when importing.
Now import all the files in the ProtocolBuffers directory and the Person.pbobjc.h and Person.pbobjc.m compiled above into the project.
Now the project directory structure looks like this:
It should be noted that since protobuf does not use ARC, you need to add -fno-objc-arc to all .m files to turn off ARC:
Notice:
Need to pay attention to the Header Search Paths in the project to increase $(PROJECT_DIR)/ProtocolBuffers (the specific path depends on the situation)
6.3 Directly import the ProtocolBuffers project
If you feel that the way to manually import files is too complicated, you can directly import the ProtocolBuffers project as a dependency:
1) Enter the decompressed protobuf directory, copy all the files in the objective directory to the ProtobufDemo/ProtocolBuffers directory;
2) Introduce the ProtocolBuffers_iOS project into the ProtobufDemo project:
3) Add dependencies and link libraries in Build Phases:
4) Import Person.pbobjc.h and Person.pbobjc.m files and add -fno-objc-arc to .m;
5) Modify some paths in the project configuration to $(PROJECT_DIR)/ProtocolBuffers.
6.4 Running the test
First import the header file:
#import "Person.pbobjc.h"
Generate Person objects and encode and decode:
Person *p = [[Person alloc] init];
p.id_p = 1;
p.name = @"person1";
p.email = @"[email protected]";
//encode
NSData*data = [p data];
NSLog(@"Protocol Buffers:\n%@\nData: %@\nData Length: %lu", p, data, data.length);
//decode
Person *newP = [[Person alloc] initWithData:data error:nil];
NSLog(@"Decoded: %@", newP);
Run the program and print the log as follows:
Protocol Buffers:
: {
name: "person1"
id: 1
email: "[email protected]"
}
Data: <0a077065 72736f6e 3110011a 0a313233 4071712e 636f6d>
Data Length: 23
Decoded: : {
name: "person1"
id: 1
email: "[email protected]"
}
Coffee time!
(This article is simultaneously published at: http://www.52im.net/thread-1515-1-1.html )