Should I apply for a patent for my invention before or after publishing the details of my invention?

you should apply for a patent before publication. Otherwise, the application may be unsuccessful because the technology is published as existing technology, which loses its novelty.

Article 24 of the Patent Law An invention-creation for which an application for a patent is filed shall not lose its novelty in any of the following circumstances within six months before the date of filing:

(1) It was exhibited for the first time at an international exhibition sponsored or recognized by the China Municipal Government;

(2) It was first published at a specified academic conference or technical conference;

(3) others disclose the contents without the consent of the applicant.